gpt4 book ai didi

javascript - Backbone Javascript - 找不到函数?

转载 作者:行者123 更新时间:2023-11-30 08:59:54 25 4
gpt4 key购买 nike

我不断收到“filterMessages 不是函数”错误。这是 Backbone View 类 - 不知道为什么找不到函数。

这是 Firebug 中的错误filterMessages 未定义第 152 行 (filterMessages("all").each(function(message) {

MessageListView = Backbone.View.extend({

defaults : {
filterString : "all"
},

initialize : function() {

this.collection.on("add", function(model) {
var view = new MessageView({model: model});
$("div.cameras").prepend(view.render().el);
});

this.collection.on("remove", function(model) {
var ID = model.id;
$("#message-" + ID).parent("div.message").remove();
});

this.collection.on("reset", function(models) {
$("div.cameras").empty();
filterMessages("all").each(function(message) {
var view = new MessageView({model: message});
$("div.cameras").prepend(view.render().el);
});

});

},

setFilter : function(filterString) {
this.filterString = filterString;
this.collection.reset(this.collection);
},

filterMessages : function(filterString) {
return this.collection.filter(function(model){

if (filterString == "all")
{
return true;
}
else if (filterString == "active")
{
return model.get("ignore") == "0";
}
else if (filterString == "ignore")
{
return model.get("ignore") == "1";
}
else if (filterString == "flag")
{
return model.get("flag") == true;
}

});
}

});

最佳答案

您需要在 View 的上下文中调用它:

initialize : function() {

var self = this;

// code here

this.collection.on("reset", function(models) {
$("div.cameras").empty();
self.filterMessages("all").each(function(message) {
var view = new MessageView({model: message});
$("div.cameras").prepend(view.render().el);
});
});
}

关于javascript - Backbone Javascript - 找不到函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10128694/

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