vendredi 31 juillet 2015

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.

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.

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);
                      }
                    });
                  }
                });
              }
            });
          });
        });
      });
    });
  }
});




Aucun commentaire:

Enregistrer un commentaire