我在初始化函数中得到一个“未定义”。我想要做的就是获取集合数据,并在成功时使用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:missing formal parameter”。 ...不知道如何解决这个问题,非常感谢你的帮助。
答案 0 :(得分:2)
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);
}
});