简体   繁体   English

Ember.js复选框和Promise

[英]Ember.js checkbox and Promise

I have an issue when trying to use a checkbox to work out if a one-to-many relationship exists. 尝试使用复选框来计算是否存在一对多关系时,我遇到问题。

I have a userPrivilege table with userId, modelId, privilegeId and scopeId. 我有一个userPrivilege表,其中包含userId,modelId,privilegeId和scopeId。

I am trying to display checkboxes which show whether the user has privileges. 我正在尝试显示复选框,以显示用户是否具有特权。 To do this, I need to check multiple conditions, not just a simple boolean field. 为此,我需要检查多个条件,而不仅仅是一个简单的布尔字段。

I can use asynchronous functions to determine whether the user has privileges, but I just don't know how to assign this to the checkbox 'selected'. 我可以使用异步函数来确定用户是否具有特权,但是我只是不知道如何将其分配给“选定”复选框。 Returning the promise means the checkbox is always checked. 返回承诺意味着始终选中该复选框。 Returning the value from within the promise always leaves it unchecked. 从promise中返回值总是使其不受检查。

See below a snippet of something I was attempting... 下面是我正在尝试的内容的摘要...

Checkbox Component: 复选框组件:

    export default Ember.Component.extend({
          user: null,
          model: null,
          privilege: null,
          scope: null,
          selected: function() {
            return this.get('authorization').hasAuthorization(
              this.get('user.id'), 
              this.get('model'), 
              this.get('privilege'), 
              this.get('scope')).then(function(result) {
              return result;
            });
          }.property()
        })

Service: 服务:

export default Ember.Service.extend({
  hasAuthorization: function(userId, model, privilege, scope) {
    var store = this.get('store');

    return new Ember.RSVP.Promise(function (resolve, reject) {
      store.find('user', userId).then(function (user) {
        user.get('userPrivileges').then(function (userPrivileges) {
          userPrivileges.find(function (userPrivilege, index, userPrivileges) {
            userPrivilege.get('model').then(function (_model) {
              if (_model.get('name') === model) {
                userPrivilege.get('privilege').then(function (_privilege) {
                  if (_privilege.get('name') === privilege) {
                    userPrivilege.get('scope').then(function (_scope) {
                      if (_scope.get('name') === scope) {
                        resolve(true);
                      }
                    });
                  }
                });
              }
            });
          });
        });
      });
    });
  }
});

Edit: As per comment, I have now tried chaining, but now _privilege and _scope are undefined when the if statement isn't met (because no promise is returned). 编辑:根据评论,我现在尝试链接,但是当不满足if语句时(因为没有返回承诺),现在_privilege和_scope是未定义的。 I think this could be done a lot better using some other structure, I just need to figure out how. 我认为使用其他结构可以做得更好,我只需要弄清楚如何做。

This now works, except that it doesn't resolve to false if the permission doesn't exist: 现在可以正常工作,但是如果权限不存在则不会解析为false:

return new Ember.RSVP.Promise(function (resolve, reject) {
      store.find('user', userId).then(function (user) {
        if (!user) {
          return resolve(false);
        }
        return user.get('userPrivileges');
      }).then(function(userPrivileges) {
        if (!userPrivileges) {
          return resolve(false);
        }
        userPrivileges.forEach(function (userPrivilege) {
          userPrivilege.get('model').then(function (_model) {
            if (_model.get('name') === model) {
              return userPrivilege.get('privilege');
            }
          }).then(function (_privilege) {
            if (_privilege.get('name') === privilege) {
              return userPrivilege.get('scope');
            }
          }).then(function (_scope) {
            if (_scope.get('name') === scope) {
              console.log('true: ' + model + ' ' + privilege + ' ' + scope);
              return resolve(true);
            }
          }).catch(function(error) {
          });
        });
      });
    });

You just need to reject when you have an errror now and resolve your outer promise to false if one of those errors occur. 您现在只需要在出现错误时就拒绝,并在出现这些错误之一的情况下将您的外在承诺变为假。

import Ember from 'ember';

export default Ember.Service.extend({
  hasAuthorization: function (userId, model, privilege, scope) {
    var store = this.get('store');

    return new Ember.RSVP.Promise(function (resolve, reject) {
      store.find('user', userId).then(function (user) {
        if (!user) {
          return resolve(false);
        }
        return user.get('userPrivileges');
      }).then(function(userPrivileges) {
        if (!userPrivileges) {
          return resolve(false);
        }
        return resolve(userPrivileges.find(function (userPrivilege, index, userPrivileges) {
          userPrivilege.get('model').then(function (_model) {
            if (_model.get('name') === model) {
              return userPrivilege.get('privilege');
            }
            return Ember.RSVP.reject();
          }).then(function (_privilege) {
            if (_privilege.get('name') === privilege) {
              return userPrivilege.get('scope');
            }
            return Ember.RSVP.reject();
          }).then(function (_scope) {
            if (_scope.get('name') === scope) {
              return Ember.RSVP.resolve(true);
            }
            return Ember.RSVP.reject();
          }).then(null, function(error) {
            return Ember.RSVP.resolve(false);
          });
        }));
      });
    });
  }
});

then takes a second argument that handles errors. then采用第二个参数来处理错误。 https://github.com/tildeio/rsvp.js/#basic-usage https://github.com/tildeio/rsvp.js/#basic-usage

When chaining, the second argument is called if any of the previous methods of the chain rejected. 链接时,如果拒绝链的任何先前方法,则调用第二个参数。

In this case we want to resolve the original promise to false if a rejection occurs. 在这种情况下,如果发生拒绝,我们希望将原始的承诺解析为false。

Update: Addressed issue where find might return undefined. 更新:解决了查找可能返回未定义的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM