我有一组模型,通过服务器调用添加到集合中。所有的模型都被添加并且在集合中追踪。现在我想要一种查找集合并根据模型内指定的id属性返回模型的方法。我不是在谈论内置的id集合,而是指集合中每个模型的自定义id。
目前我有这个代码,但我的_detect函数没有返回我想要的结果。
目前我有这个代码,但我的_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);
}
});
});
idAttribute
将其设置为模型的id
吗? - Crescent Fresh