简体   繁体   中英

Backbone.js Undefined in Router initialize function

I'm getting an "undefined" in the initialize function. All I want to do is to fetch the collection data and on success use html() to put it in the DOM.

This is my Router:

var OverviewApp = new (Backbone.Router.extend({
    routes : {"": "times_day", "weeks":"times_week"},

    initialize: function(){
        this.dayCollection = new DayCollection({});
        this.weekCollection = new WeekCollection({});

        this.dayTableView = new CollectionDayView({collection: this.dayCollection});
        this.weekTableView = new CollectionWeekView({collection: this.weekCollection});

        this.dayCollection.fetch({
            success: function(){
                this.dayTableView.render();    <--- HERE
                $("#times_day").html(this.dayTableView.el);
            }
        });
    },

Firebug says "this.dayTableView" is undefined. It is understandable cause it is not in the function context, then I tried doing:

this.dayCollection.fetch({
            success: function(this.dayTableView){
                this.dayTableView.render();
                $("#times_day").html(this.dayTableView.el);
            }
        });

But now the error is "SyntaxError: missing formal parameter". ... Not sure how to solve this, thanks a lot for your help.

this

    this.dayCollection.fetch({
        success: function(){
            this.dayTableView.render();
            $("#times_day").html(this.dayTableView.el);
        }.bind(this)
    });

or

    var that = this;
    this.dayCollection.fetch({
        success: function(){
            that.dayTableView.render();
            $("#times_day").html(that.dayTableView.el);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM