来自承诺的计算财产

the*_*ack 1 ember.js ember-data

我有一个关系(很多)的余烬数据模型.所以有一个计算属性取决于关系元素.我需要根据关系属性的数据返回布尔值.

isAllowed: function(){
        return this.get('allowedUsers').then(function(data){
            var user = this.get('currentUser');
            return data.contains(user);
        }.bind(this));
}.property()
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,isAllowed是计算属性并且allowedUsersuser模型有许多关系async并被设置为true.isAllowed被设置为promise对象而不是返回布尔值.我甚至试图创建另一个承诺对象,如下面但没有运气.

 isAllowed: function(){
            return new Ember.RSVP.promise(function(resolve, reject){
               this.get('allowedUsers').then(function(data){
                var user = this.get('currentUser');
                 resolve(data.contains(user));
              }.bind(this));

            }.bind(this));
    }.property()
Run Code Online (Sandbox Code Playgroud)

如何确保将isAllowed设置为布尔值而不是promise对象?

tok*_*rev 5

试试这个:

isAllowed: function(key, value){
  if (arguments.length > 1) return value; // the setter was called

  this.get('allowedUsers').then(function(data){
    var user = this.get('currentUser');
    this.set("isAllowed", data.contains(user));
  }.bind(this));

  return false;
}.property()
Run Code Online (Sandbox Code Playgroud)