简体   繁体   English

Backbone.js在路由器初始化函数中未定义

[英]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. 我要做的只是获取收集数据,成功使用html()将其放入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. Firebug说“ this.dayTableView”是未定义的。 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". 但是现在错误是“ SyntaxError:缺少形式参数”。 ... 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);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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