1

我不断收到“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;
            }

        });
    }

});
4

2 回答 2

5

您将希望在视图的上下文中调用它:

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);
        });
    });
}
于 2012-04-12T17:32:49.690 回答
0

只需将“this”定义为内部函数外部的变量,因为在内部函数内部时它将采用不同的属性。

primaryFunction : function() {
    var self = this;

    innerFunction : function(filterString) {
        self.someOtherFunction();
    };
}
于 2016-11-14T12:26:22.727 回答