在集合中查找模型
我有一组模型通过服务器调用添加到集合中。我的所有模型都已添加并正在追踪中以纳入集合中。现在我想要一种方法来查找集合并根据模型内指定的 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);
}
});
});
I have a set of models that are added to a collection via a server call. All my models are added and are tracing out to be in the collection. Now I want a way to lookup the collections and return a model based on a specified id attribute inside the model. I'm not talking about the collections built in id. I'm refering to a custom id that is part of each model in the collection.
So fare I have this. but my _detect function is not returning what I'm after.
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);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,以防万一其他人需要答案。
Ok figured it out just in case anyone else needs the answer.