theironcook / Backbone.ModelBinder

Simple, flexible and powerful Model-View binding for Backbone.
1.42k stars 159 forks source link

_isSetting flag race condition when we're trying to change an input class based on input value #230

Open fcamblor opened 8 years ago

fcamblor commented 8 years ago

Hi,

I just hit an issue while defining multiple binder on the same field.

Typically, if I have following input field :

<div id="content">
  <input type="text" name="foo" />
</div>

and I define following model binder :

    this._modelBinder.bind(model, this.el, {
      'foo': [{
        selector: '[name=foo]'
      }, {
        selector: '[name=foo]',
        elAttribute: 'class',
        converter: function(model, value) {
          console.log("class elAttribute triggered for foo");
          return (value && value.length>3)?"error":undefined;
        }
      }]
  });

The "class elAttribute triggered for foo" log is only displayed during model binder initialization, but never after (typically, when my foo attribute is modified)

This is due to the _isSetting lock put on target el (which is the same between both elementBinding in our case) :

I don't see how we could elegantly workaround this issue.

Anyway, if I use a DOM wrapper around my input for handling this class change, it works perfectly.

See following jsfiddle which demonstrates the issue and the workaround :

Regards,