gpt4 book ai didi

javascript - Backbone 无法使用 "this"?

转载 作者:行者123 更新时间:2023-11-28 03:32:46 25 4
gpt4 key购买 nike

我只是努力将下划线 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com