我有一组模型,它们通过服务器调用添加到集合中。我所有的模型都已添加并正在追踪以加入收藏。现在我想要一种方法来查找集合并根据模型中指定的 id 属性返回模型。我不是在谈论内置于 id 中的集合。我指的是属于集合中每个模型的自定义 ID。
太好了,我有这个。但是我的 _detect 函数没有返回我想要的。
var collection = Backbone.Collection.extend({
initialize: function( ) {
_.bindAll(this);
this.bind('add', this.modelIsAddedd);
this.serverCall();
},
modelIsAddedd: function(model){
console.log('model = ', model);
},
getModelByCustomID: function( id ){
var model = this.detect( id, function( model ){ return model });
},
serverCall: function(){
$.ajax({
my ajax call with success and error
});
},
onSuccess: function(response){
this.add(response.data);
}
});
});
最佳答案
好的,以防万一其他人需要答案。
getModelByCustomID: function( id ){
var model = this.detect( function( model ){
return model.get('customIDName') == id;
});
},
关于javascript - 在集合中查找模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840902/