【发布时间】:2012-04-19 12:38:35
【问题描述】:
在学习 Backbone.js 的同时,我正在使用 Todos 示例应用程序 bundled with the latest version of Backbone (0.9.2)。我的问题是,为什么应用程序设计为在向 Todos 集合添加模型时触发两次渲染事件?
如果我将这一行放在 TodoView 的渲染函数中:
// Re-render the titles of the todo item.
render: function() {
console.log("Rendering!");
this.$el.html(this.template(this.model.toJSON()));
然后是“渲染!”在控制台中出现两次。我理解这是因为视图将模型的更改事件绑定到视图的渲染:
initialize: function() {
this.model.bind('change', this.render, this);
并且在addOne中调用了render,绑定了Todos的add事件:
addOne: function(todo) {
var view = new TodoView({model: todo});
this.$("#todo-list").append(view.render().el);
},
但是这种双渲染设计是标准做法吗?看起来视图应该在创建(或进入 DOM)时呈现,然后在底层模型发生变化时再次呈现。在这种情况下,什么都没有改变,但是渲染被调用了两次。
再说一次,我只是在学习 Backbone,所以我可能有一个基本的误解导致我的困惑。
【问题讨论】: