使用属性过滤骨干集合

ped*_*ete 1 collections filter backbone.js

我有一个骨干集合,我试图基本上在属性中过滤id,用户有类,并且类有一个location_id,我想按位置id过滤.我的收藏看起来像这样给你一个想法.

-user
   -models
     -0
       -attributes
         -location_id
      -1
       -attributes
         -location_id
      -2
       -attributes
         -location_id

我以为我可以通过使用来过滤它

  var get_locations = user_class_collection.filter(function(classes){
      console.log(classes); 
       return classes.get(location_id)==location.id;
       });

  console.log(get_locations);

但是当我知道location_id在集合中时,它返回一个空数组.知道为什么这不起作用吗?我也试图抓住classes.attributes.get,但它没有任何好转

在前几个回复中,恰当地提到我必须引用它get('location_id').我现在已经这样做了,但不幸的是,我仍然得到一个空阵列.我认为filter会循环遍历类,我会得到每个类的控制台输出,但console.log(classes)只会被触发一次.这是暗示吗?还是红鲱鱼?

San*_*der 6

你试图从名为location_id参数的值的类中获取属性,你应该将其作为一个字符串(实际上你可以选择如何使它成为字符串,单引号或双引号都工作)

user_class_collection.filter(function(classes){
    return classes.get('location_id') == location.id;
});
Run Code Online (Sandbox Code Playgroud)