Reputation: 861
This may be a result of misuse of the component, 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!
Upvotes: 2
Views: 5637
Reputation: 1446
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.com/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.
Upvotes: 0
Reputation: 434685
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, returnstrue
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.
Upvotes: 8