goj*_*ygo 3 javascript backbone.js
当我尝试通过模型中save的更改事件更新Backbone中的模型时,会触发两次.事件按此顺序触发.
但是,如果我通过(wait: true),则只change触发第一个事件.
任何人都可以解释为什么会发生这种情况,以及如何change在不通过的情况下只开火一次(wait: true)?
两个变化事件:
editItem: function() {
this.model.save({
name: "Howard the Duck"
});
}
Run Code Online (Sandbox Code Playgroud)
一个变化事件:
editItem: function() {
this.model.save({
name: "Howard the Duck"
}, (wait: true);
}
Run Code Online (Sandbox Code Playgroud)
伪代码:
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");
}
});
Run Code Online (Sandbox Code Playgroud)
以下是在更改属性时保存模型时发生的情况的分解:
model.set使用作为参数传递的数据options.waitmodel.set从服务器返回的数据options.wait,或者服务器返回的数据不是模型知道的数据您的服务器可能会返回一个修改后的属性,触发第二个更改事件 修改您的回复或限制您的事件倾听
this.model.on("change:name", this.changing, this);
Run Code Online (Sandbox Code Playgroud)
比较这些小提琴
http://jsfiddle.net/nikoshr/ZWFWx/
响应是客户端发送给
你的应该看到的变化 - 请求 - 同步
http://jsfiddle.net/nikoshr/QT3YJ/
模拟您的问题的版本,其中服务器添加模型
更改中不存在的值- 请求 - 更改 - 同步
| 归档时间: |
|
| 查看次数: |
3620 次 |
| 最近记录: |