【发布时间】:2010-12-27 22:55:17
【问题描述】:
这是我第一次尝试使用 Backbone.js,所以我决定制作一个简单的测试应用程序,用菜单项模拟餐厅菜单。我跟着this cool blog post from andyet.net。
将模型添加到集合时遇到问题。我已将视图方法绑定到应该更新视图的集合的添加事件。这是尽可能多地删除不相关代码的代码。
(注意:为简洁起见,我删除了局部范围 var 声明和闭包之类的内容。以下内容仍然有点长,我知道这很烦人,但它应该非常简单易懂明白):
MenuItem = Backbone.Model.extend({
// initialize functions removed for brevity
});
Menu = Backbone.Model.extend({
// initialize functions removed for brevity
// MenuSelection property is defined here.
});
MenuSelection = Backbone.Collection.extend({ model: MenuItem });
MenuItemView = Backbone.View.extend({
// render template
});
/**
* This is unaltered from the code. The error occurs here.
*/
RestaurantAppView = Backbone.View.extend({
addMenuItem : function (MenuItem) {
var view = new MenuItemView({ model : MenuItem });
this.menuList.append(view.render().el);
// ERROR occurs here. error: "this.menuList is undefined"
},
initialize : function () {
this.model.MenuSelection.bind('add', this.addMenuItem);
},
render : function () {
$(this.el).html(ich.app(this.model.toJSON()));
this.menuList = this.$('#menuList'); // IT IS DEFINED HERE
return this;
}
});
/**
* Everything after this I left in just-in-case you need to see it.
*/
RestaurantAppController = {
init : function (spec) {
this.config = { connect : true };
_.extend(this.config, spec);
this.model = new Menu({
name : this.config.name,
});
this.view = new RestaurantAppView({ model : this.model });
return this;
}
};
$(function() {
// simulating ajax JSON response.
var json = {
Menu : {
name : 'Appetizers',
MenuItem : [
{
name : 'toast',
description : 'very taosty',
price : '$20.00'
},
{
name : 'jam',
description : 'very jammy',
price : '$10.00'
},
{
name : 'butter',
description : 'very buttery',
price : '$26.00'
}
]
}
};
window.app = RestaurantAppController.init({
name : json.Menu.name
});
$('body').append(app.view.render().el);
app.model.MenuSelection.add(json.Menu.MenuItem);
});
我已经用 cmets 标记了有问题的区域。根据Backbone Documentation:
如果页面中包含 jQuery 或 Zepto,则每个视图都有一个 $ 函数,用于运行视图元素范围内的查询。
那么,如果我在渲染方法中设置了this.menuList = this.$('#menuList');,为什么我不能在addMenuItem 方法中访问this.menuList?我在顶部链接到的演示就是这样。另外,如果我将 this.menuList 换成 jQuery 选择器,如下所示:
addMenuItem : function (MenuItem) {
var view = new MenuItemView({ model : MenuItem });
$('#menuList').append(view.render().el);
}
一切正常。但我不想要在每次执行 addMenuItem 时重新选择菜单列表 ID。 RightWayTM是在渲染后缓存。
另请注意:我认为问题可能在于 ICanHaz 模板返回的速度不够快,但是 this.menuList 将是一个空数组,而不是 undefined,所以不是这样。
【问题讨论】:
标签: javascript jquery view undefined backbone.js