如何过滤所有没有特定项目的记录(资源模型)



基本上我有项目模型和资源模型。项目有许多资源,资源有许多项目(多对多关系)。所以我想查看全部或过滤不属于我的项目的资源。像这样:

this.store.filter('resource', function(resource){
resource.get('projects') != this.get('project')
}) 

应该是这样的。我不知道什么是正确的方法。我只想获取那些没有this.get('project')或特定项目的资源。

您可以使用

组合peekAllrejectBy

var resourcesThatDoNotBelongToMyProject = this.get('store').peekAll('resources').rejectBy('projects', this.get('project'));

请注意,rejectBy的反面是findBy

它将返回所有不属于我的project的活动资源

 resourcesThatDoNotBelongToMyProject: Ember.computed('resources',function(){
        var _self = this;
        return this.get('resources').filter(function(resource){
           let not_present=true;
           if(resource.get('projects') != undefined) {
               resource.get('projects').forEach(function (project) {
                   if (project.id  == _self.get('model').id) {
                       not_present = false;
                   }
               });
           }
            if(not_present == true) {
                return resource.get("active") == true
            }
       });
    }),

最新更新