【发布时间】:2012-12-22 01:26:40
【问题描述】:
当我尝试通过save 更新 Backbone 中的模型时,模型上的更改事件会触发两次。事件按此顺序触发。
- 改变
- 请求
- 改变
- 同步
但是,如果我通过 (wait: true),则只会触发第一个 change 事件。
谁能解释为什么会发生这种情况,以及如何让change 只触发一次而不通过(wait: true)?
两个变更事件:
editItem: function() {
this.model.save({
name: "Howard the Duck"
});
}
一个变更事件:
editItem: function() {
this.model.save({
name: "Howard the Duck"
}, (wait: true);
}
伪代码:
App.Views.Item = Backbone.View.extend({
initialize: function() {
this.model.on("change", this.changing, this);
this.model.on("request", this.requesting, this);
this.model.on("sync", this.syncing, this);
},
events: {
"click a": "editItem"
},
editItem: function() {
this.model.save({
name: "Howard the Duck"
});
}
changing: function() {
console.log("changing");
},
requesting: function() {
console.log("requesting");
},
syncing: function() {
console.log("syncing");
}
});
【问题讨论】: