简体   繁体   English

骨干ModelBinder和jQuery插件

[英]Backbone ModelBinder and jQuery Plugin

In an application written with Backbone & Marionette, I want some of the form inputs as numeric only and with thousands separator. 在用Backbone&Marionette编写的应用程序中,我希望某些表单输入仅作为数字并带有千位分隔符。 Backbone ModelBinder automatically detects changes in the form. Backbone ModelBinder自动检测表单中的更改。 I implemented jQuery number plugin which works fine. 我实现了很好的jQuery数字插件 But the problem is that when there is a thousands separator in a numeric input, ModelBinder doesn't work. 但是问题在于,当数字输入中包含数千个分隔符时,ModelBinder无法正常工作。 When there is less than 4 digits (without a separator everything is ok. 如果少于4位数字(不带分隔符,则表示一切正常)。

The problem occurs on Chrome. 该问题在Chrome上出现。 There isn't any problem on Firefox. 在Firefox上没有任何问题。

I don't have any clue how to solve or debug the problem. 我不知道如何解决或调试问题。

You're asking for trouble by combining the two: model binder triggers change events when inputs change and forwards the fields data to the model. 您需要通过将两者结合来解决问题:当输入发生更改时,模型绑定器会触发更改事件,并将字段数据转发至模型。 Except it has been tampered with by the number plugin, so issues arise. 除非它已被数字插件篡改,否则会出现问题。

Instead, try using ModelBinder's converter binding settings ( https://github.com/theironcook/Backbone.ModelBinder#formatting-and-converting-values ), it will allow you to defined how data should be formatted/parsed when going from the model to the form and back. 相反,请尝试使用ModelBinder的转换器绑定设置( https://github.com/theironcook/Backbone.ModelBinder#formatting-and-converting-values ),它将允许您定义从模型中获取数据时应如何格式化/解析到表格并返回。

Use ModelBinder's converters for this instead of the jQuery plug-in. 为此,请使用ModelBinder的转换器,而不要使用jQuery插件。 Here's an example that cleans up a time (eg 3p, 3:00, 3PM, 15:00, 1500, etc.), stores the data in the model in a canonical form (time portion of ISO8601) if the input can be parsed, and if not then it is stored as is so that validation can check it separately and provide an error. 这是清理时间(例如3p,3:00、3PM,15:00、1500等)的示例,如果可以解析输入,则以规范形式(ISO8601的时间部分)将数据存储在模型中,如果没有,则按原样存储,以便验证可以单独检查它并提供错误。

ModelBinder's converters get called twice when a user changes an input. 用户更改输入时,ModelBinder的转换器被调用两次。 First when the input data is copied from the view to the model, direction === 'ViewToModel' . 首先,将输入数据从视图复制到模型时, direction === 'ViewToModel' This allows for cleanup to occur and to transform the input value into a canonical form suited for storage, eg in 24-hour time with seconds ( 15:30:00 ) in this example. 这样可以进行清理,并将输入值转换为适合存储的规范形式,例如,在本示例中为24小时制,秒为( 15:30:00 )。 And secondly, from the model back to the view, direction === 'ModelToView' , which allows you to present the data to the user in a friendly manner, in 12-hour time ( 3:30 PM ) in this example. 其次,从模型返回到视图, direction === 'ModelToView' ,在此示例中,它允许您以友好的方式向用户展示数据,时间为12小时( 3:30 PM )。

This example uses a time library to manipulate the time input, parse it, and format it. 本示例使用时间库来操纵时间输入,对其进行解析并对其进行格式化。

Binding 捆绑

In this case onRender is called right after the view is rendered using Backbone.Marionette 在这种情况下,在使用Backbone.Marionette渲染视图之后立即调用onRender

onRender: function() {
  var bindings = ModelBinder.createDefaultBindings(this.$el, 'name');
  bindings.value.converter = ModelSaver.timeStringConverter;
  this.modelbinder.bind(this.model, this.$el, bindings);
}

Converter 变流器

ModelSaver.timeStringConverter = function(direction, value, attributeName, model, els) {
    var result;

    if (direction === 'ViewToModel') {
      if (!value)
        // if the input is empty, just pass it along so it can be deleted, and/or validation can indicate it was a required field
        result = value;
      else {
        // parse the input value and store it with the second component only if it's valid, if not, store the invalid value so that model validation can catch it
        result = new Time(value);
        result = result.isValid() ? result.format('HH:mm')+':00' : value;
      }
    }

    if (direction === 'ModelToView') {
      // chop off the seconds, parse, check for validity, and format
      result = value && new Time(value.substr(0,5));
      result = (value && result.isValid()) ? result.format('h:mm AM') : value;
    }

    return result;
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM