[英]Backbone.ModelBinder: why do I have to hit submit twice?
I am using Backbone and Backbone.ModelBinder. 我正在使用Backbone和Backbone.ModelBinder。
I have a bunch of text fields that are bound via BackboneModelBinder. 我有一堆通过BackboneModelBinder绑定的文本字段。 Everything works as expected however when I make a change to a text field and I don't unfocus the field first (click off the input field) before hitting the SAVE button, I have to hit the Save button twice -- once to unfocus the fields, and then a second time to actually fire the save button handler (which should have fired the first time) 一切都按预期进行,但是当我更改文本字段并且在单击“保存”按钮之前不先取消对字段的聚焦(单击输入字段)时,我必须两次单击“保存”按钮-一次使焦点不清晰字段,然后第二次实际触发保存按钮处理程序(应该是第一次触发)
(Save is a standard html button with a backbone event bound to it). (“保存”是一个标准html按钮,绑定了一个骨干事件)。
Does anyone have any knowledge of why this might be? 有谁知道为什么会这样吗?
I hope this made sense :| 我希望这是有道理的:
Thanks for any help or direction 感谢您的帮助或指导
-Kirk -柯克
That's because ModelBinder by default set the new value to the model's attributes on "blur" or "change" events (it dependes on the input's type). 这是因为默认情况下,ModelBinder在“模糊”或“更改”事件(取决于输入的类型)上将新值设置为模型的属性。 You can modify this behavior by changing the source code, adding keyup as binding event in those two methods: 您可以通过更改源代码,在这两种方法中将keyup作为绑定事件添加来修改此行为:
_bindViewToModel:function () {
$(this._rootEl).delegate('', 'change keyup', this._onElChanged);
// The change event doesn't work properly for contenteditable elements - but blur does
$(this._rootEl).delegate('[contenteditable]', 'blur keyup', this._onElChanged);
},
_unbindViewToModel: function(){
if(this._rootEl){
$(this._rootEl).undelegate('', 'change keyup', this._onElChanged);
$(this._rootEl).undelegate('[contenteditable]', 'blur keyup', this._onElChanged);
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.