我只是很难在主干集合中使用下划线get。
var collection=Backbone.Collection.extend({
model:someModel,
getModelEntry : function(id){
return this.get(id);
//returns undefined
}
})
尝试2:
var collection=Backbone.Collection.extend({
model:someModel,
getModelEntry : function(id){
var model = this.where({id:id})[0];
//here I got model
return model.get("attr");
//returns undefined
}
});
使用get in collection有什么问题?
get在实例上运行得很好!
var coll=new collection;
coll.get(id); //working fine
最佳答案
在我看来效果不错。检查您正在寻找的模型的ID是否存在于您的集合中。添加如下内容,看看会发生什么
getModelEntry : function(id){
var model = this.get(id);
if(model == undefined) {
console.log("id: ",id);
console.log("collection: ",JSON.stringify(this.models));
} else {
console.log(model.get('name'));
}
}
关于javascript - Backbone 不能使用“this”吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16559007/