admin管理员组

文章数量:1406149

This may be a result of misuse of the ponent, though I don't think so. I have an issue where a View updates a model in Backbone JS and calls the model's Set method so that it may verify it's input. In theory there are two results to such an action: Error and Change. Both events work as prescribed. But in fact there is a third event: No change. That is, if the input has not been changed at all, I can't tell after calling Set because no error will be thrown but nor will a change event, as nothing has actually changed- but I still want to know about such a case. Is there a way for me to do this?

The reason is that there is an action I want performed only if no error occurs, but there is no way for me to know (without a change event) that the model has attempted to set the new values and ended with no result as it all happens asynchronously.

Thanks!

This may be a result of misuse of the ponent, though I don't think so. I have an issue where a View updates a model in Backbone JS and calls the model's Set method so that it may verify it's input. In theory there are two results to such an action: Error and Change. Both events work as prescribed. But in fact there is a third event: No change. That is, if the input has not been changed at all, I can't tell after calling Set because no error will be thrown but nor will a change event, as nothing has actually changed- but I still want to know about such a case. Is there a way for me to do this?

The reason is that there is an action I want performed only if no error occurs, but there is no way for me to know (without a change event) that the model has attempted to set the new values and ended with no result as it all happens asynchronously.

Thanks!

Share Improve this question edited Jan 8, 2012 at 22:34 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Nov 20, 2011 at 20:51 Che KofifChe Kofif 8611 gold badge9 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Every Backbone model has a hasChanged method:

hasChanged model.hasChanged([attribute])

Has the model changed since the last "change" event? If an attribute is passed, returns true if that specific attribute has changed.

Perhaps you can use that to check your third possibility.

BTW, the callbacks aren't asynchronous. The error and changed callbacks are triggered and return before set returns:

set : function(attrs, options) {
  //...

  // Run validation.
  if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;

  //...

  // Update attributes.
  for (var attr in attrs) {
    var val = attrs[attr];
    if (!_.isEqual(now[attr], val)) {
      now[attr] = val;
      delete escaped[attr];
      this._changed = true;
      if (!options.silent) this.trigger('change:' + attr, this, val, options);
    }
  }

The _performValidation call triggers the error callbacks, the this.trigger calls will call the per-attribute callbacks.

In this case, you may need to dance around Model.set() a little bit to get where you want. If you are using this functionality, then you should have defined a validate() method on your model.

http://documentcloud.github./backbone/#Model-validate

So you can call this method directly...

// something happens and we need to update the model to "newvalues"
if (model.validate(newvalues)) {
  model.trigger('error')
} else {
  model.trigger('change')
}
model.set(newvalues)

That way you will always at least get 'change' or 'error' out of it, even if it's the same. You will also still get the existing events from set.

本文标签: javascriptHow can I tell if a model has not been changed in BackbonejsStack Overflow