Ember.js:如何查找没有属性集的记录?

时间:2013-02-14 21:35:55

标签: javascript ember.js

我有一堆记录“belongsTo”其他记录。 (不同的模型,只是典型的1-n关系。)如何在模型上调用find来查找所有没有属于关联的东西?

类似的东西:

App.Thing = DS.Model.extend({
 other: DS.belongsTo( 'App.Other' ),
 someattr: DS.attr('string')
});

App.Thing.find({ other: null });

修改

louisquio的解决方案对我不起作用。所以我以为我可以在控制器中过滤。

以下是我尝试这样做的方法:

App.ThingsAsideController = Ember.ArrayController.extend({
  unassigned: function() {
   return this.filterProperty('other');
  }.property('content.@each')
});

如果我过滤someattr但我无法过滤关系,这样就可以了。

我想问题是:belongsTo协会的属性名称是什么?

编辑,有些工作:

这很有效,但我对它非常不满意:

 App.ThingsAsideController = Ember.ArrayController.extend({
   unassigned: function() {
     return this.filter(function(item, index, enumerable){

       var belongsToOther = false;

       var otherThings = App.OtherThing.find();
       otherThings.forEach( function( otherThing ){
         otherThing.get('things').forEach( function( otherThingThing ) {
            if( otherThingThing.id === item.id ) {
              belongsToOther = true;
            }
         });
       });

       return !belongsToOther;
    });

我一直在阅读余烬数据源,但我找不到如何查询记录的所属关联。

1 个答案:

答案 0 :(得分:2)

您可以使用DS.Model类可用的filter方法:

var filteredThings = App.Thing.filter(function(thing) {
  return thing.get('other') === null;
});

它返回一个数组,其中包含您传递的回调中返回App.Thing的所有true

正如文件所说:

  

它会返回一个实时RecordArray,它会在新记录加载到商店或创建时保持最新状态       本地。