Skip to content

Commit

Permalink
don't trigger change event if noclobber: true
Browse files Browse the repository at this point in the history
in the case where a refresh call returns, and there's un-persisted
changes on the model, we don't want to trigger a change event for those
properties.

pertains to #14
  • Loading branch information
aaronj1335 committed Feb 11, 2013
1 parent 2e71883 commit d432ae6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 8 additions & 4 deletions js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,25 +498,29 @@ define([
});

Model.prototype._setOne = _.wrap(Model.prototype._setOne,
function(f, prop, newValue, currentValue, opts) {
function(f, prop, newValue, currentValue, opts, ctrl) {
var i, l, args = Array.prototype.slice.call(arguments, 1),
inFlight = this._inFlight.save;
if (opts.noclobber) {
if (this._changes[prop]) {
return;
ctrl.silent = true;
}
for (i = 0, l = inFlight.length; i < l; i++) {
if (inFlight[i].changes[prop]) {
return;
ctrl.silent = true;
}
}
if (ctrl.silent) {
return;
}
}
if (opts.validate) {
var field = this._fieldFromPropName(prop);
try {
field.validate(newValue);
} catch (e) {
return e;
ctrl.error = e;
return;
}
}
return f.apply(this, args);
Expand Down
13 changes: 13 additions & 0 deletions js/tests/test_model_consistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,5 +1124,18 @@ define([
});
});

asyncTest('setting changed value with noclobber doesnt trigger change', function() {
setup().then(function(c) {
var m = c.first(), changes = [];
m.on('change', function(eventName, changed) {
changes.push(changed);
});
m.set('required_field', 'foobar');
m.set('required_field', m.previous('required_field'), {noclobber: true});
equal(changes.length, 1);
start();
});
});

start();
});

0 comments on commit d432ae6

Please sign in to comment.