我只是努力将下划线 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 有什么问题?
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/