English 中文(简体)
Backbone Javascript -Function notborn?
原标题:Backbone Javascript - Function not Found?

我保留“过滤器不是功能”错误。 这里是“后一观点”——它为什么不能找到这一功能。

Here s the error in Firebug filterMessages is not defined Line 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;
            }

        });
    }

});
最佳回答

You will want to call it within the context of the 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);
        });
    });
}
问题回答

仅将“这”定义为你内部职能之外的变数,因为在你内部职能范围内,它将拥有不同的财产。

primaryFunction : function() {
    var self = this;

    innerFunction : function(filterString) {
        self.someOtherFunction();
    };
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签