简体   繁体   English

Backbone.ModelBinder:为什么我必须点击两次提交?

[英]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.

相关问题 Backbone.modelBinder-无法在'keypress'上绑定 - Backbone.modelBinder - unable to bind on 'keypress' 为什么Backbone.Modelbinder只绑定视图列表的最后一个实例 - Why does Backbone.Modelbinder only bind the last instance of a list of views 我可以使用Backbone.ModelBinder将选择框选项文本绑定到模型吗? - Can I bind a select box option text to a model using Backbone.ModelBinder? 使用 Backbone.ModelBinder 向元素添加属性值的类 - Add class to elements for values of attribute with Backbone.ModelBinder 为什么我必须按两次返回才能回到Backbone? - Why do I have to press back twice to go back in Backbone? 骨干ModelBinder和jQuery插件 - Backbone ModelBinder and jQuery Plugin 为什么我必须在脚本代码之外添加$(function()…)?Javascript,骨干网,Jquery - Why do I have to add $(function()…) outer of my script code?Javascript, backbone, Jquery Backbone.js为什么我必须使用下划线(_.each)定位模型? - Backbone.js why do I have to use underscore (_.each) to target the model? 骨干模型绑定器转换器ajax请求 - backbone modelbinder converter ajax request 骨干表格如何在Backbone休息中提交生成的表单? - backbone-forms how do I submit the generated form in Backbone rest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM