0

在我的应用程序中,我需要根据这样的多对多关系授予用户访问组织的权限:

App.User = DS.Model.extend({
  fullname: DS.attr('string'),
  password: DS.attr('string'),
  administrator: DS.attr('boolean'),
  organisation_users: DS.hasMany('organisation_user', {async: true})
});

App.OrganisationUser = DS.Model.extend({
  organisation: DS.belongsTo('organisation', {async: true}),
  user: DS.belongsTo('user', {async: true}),
  administrator: DS.attr('boolean')
});

App.Organisation = DS.Model.extend({
  fullname: DS.attr('string', {defaultValue: 'Unnamed University'}),
  description: DS.attr('string'),
  organisation_users: DS.hasMany('organisation_user', {asynch: false}),
});

我正在使用 Ember SimpleAuth 进行身份验证。因此,基于对上一个问题的回答,我在我的 OrganisationController 上实现了 isAuthorised,如下所示:

App.OrganisationController = Ember.Controller.extend({

  //Determine whether the logged in user is authorised
  isAuthorised: function() {
    if(!this.get('session.isAuthenticated')) {
      console.log("Not logged in...");
      return false;
    }

    console.log("Does user [" + this.get('session.user.id') + "] have access to organisation [" + this.get('model.id') + "]?");
    if(this.get('session.user.administrator')) {
      console.log("Yes, because they are an administrator.");
      return true;
    } else {
      console.log("Well, they are not an administrator so...");
    }
    console.log("We need to check " + this.get('model.fullname') + " for authorised users.");
    this.get('model.organisation_users').forEach(function(org_user){
      org_user.get('user').then(function(user) {
        console.log(user.get('fullname') + " has access");
      });
    });
  }.property('model', 'session.user'),

问题是,我不知道如何从中返回值。此外,当我加载 OrganisationRoute 时,它​​似乎工作正常,但是当我加载不同的路线并过渡到这条路线时,它失败了

Uncaught TypeError: Cannot read property 'resolve' of undefined

Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.

4

1 回答 1

0

我认为您的错误源于这段代码(我认为您遇到了一些侧载问题):

this.get('model.organisation_users').forEach(function(org_user){
  org_user.get('user').then(function(user) { // org_user hasn't loaded its user record?
    console.log(user.get('fullname') + " has access");
  });
});

我可能会采用这样的模式:

return this.get('organisation_users').any(function (item) {
  return item.get('user.id') === this.get('session.user.id');
});

.any将枚举直到回调返回 true。如果为真,外部作用域将返回真,这就是你想要的。

如果没有匹配项,则为假。很简单。

正如我前面在答案中提到的,这个难题的另一部分是侧载。

于 2014-08-07T19:38:15.653 回答