我正在开发一个 jQuery Backbone.js Web 应用程序。
正如在 Adobe Flex 中一样,我在我的应用程序中为输入元素/小部件实现了 2 路数据绑定。因此,每个输入元素/小部件都知道其对应的模型和模型属性名称。
当用户点击 tab 或 enter 时,字段值会自动提供给模型。
container.model.set(this.attrName, this.value, options); // command 1
另一方面,当模型从后端更新时,输入元素/小部件的视图应该会自动更新:
container.model.bind("change:"+ this.attrName, this.updateView, this); // command 2
问题是:
当用户点击回车并自动更新模型时,也会触发“change:abc”并调用this.updateView,而不仅仅是当新模型来自后端时。
到目前为止,我的解决方案是在用户按下 enter(命令 1)时设置模型值时传递一个选项“source:gui”,并在我的 updateView 方法中检查它。但我不再满足于这个解决方案。
有人有更好的解决方案吗?非常感谢
沃尔夫冈
Update:
When the option silent: true is passed, the validate method of the model is not called, so that does not help. 请参阅 Backbone.js 源 0.9.2:
_validate: function(attrs, options) {
if (options.silent || !this.validate) return true;