javascript - Backbone 无法使用 "this"?

标签 javascript jquery html backbone.js

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

相关文章:

javascript - 每次我在输入框中按键盘输入按钮(其中有文本)时都会出现新段落

javascript - 我的 Canvas 游戏中有不同的 'states'

javascript - 如何使用 javascript 更改所有文本和背景的颜色?

jquery - jqGrid在EditForm中填充选择

html - 正确显示 jQuery UI 图标

iphone - 如何让我的固定宽度移动网站始终出现 "fully zoomed in"?

javascript - 使用 JavaScript 循环查找数组中的最大数字

javascript - 为什么 e.stopPropagation() 不起作用?

javascript - 如何在解析 json 时跳过 <br> 或任何字符串

javascript - 如何使用 br 断行制作(复制到剪贴板)