【问题标题】:Backbone.js Undefined in Router initialize function路由器初始化函数中未定义 Backbone.js
【发布时间】:2015-01-12 11:04:37
【问题描述】:

我在初始化函数中得到一个“未定义”。我要做的就是获取集合数据,并在成功时使用 html() 将其放入 DOM。

这是我的路由器:

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 说“this.dayTableView”未定义。这是可以理解的,因为它不在函数上下文中,然后我尝试这样做:

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

但现在错误是“SyntaxError:缺少形式参数”。 ...不知道如何解决这个问题,非常感谢您的帮助。

【问题讨论】:

  • 您的 DayCollection 定义是什么样的?

标签: javascript backbone.js


【解决方案1】:

this

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

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

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2012-05-07
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2012-11-10
    • 2018-07-12
    相关资源
    最近更新 更多