fixing validation & internal change flag order

This commit is contained in:
Tim Griesser
2012-12-27 01:16:08 -05:00
parent 61021d31e2
commit e12e1d8d45
2 changed files with 21 additions and 3 deletions

View File

@@ -304,6 +304,9 @@
options || (options = {});
// Run validation.
if (!this._validate(attrs, options)) return false;
// Extract attributes and options.
unset = options.unset;
silent = options.silent;
@@ -317,9 +320,6 @@
}
current = this.attributes, prev = this._previousAttributes;
// Run validation.
if (!this._validate(attrs, options)) return false;
// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];

View File

@@ -213,6 +213,24 @@ $(document).ready(function() {
equal(a.id, undefined, "Unsetting the id should remove the id property.");
});
test("#2030 - set with failed validate, followed by another set triggers change", function () {
var attr = 0, main = 0, error = 0;
var Model = Backbone.Model.extend({
validate: function (attr) {
if (attr.x > 1) {
error++;
return "this is an error";
}
}
});
var model = new Model({x:0});
model.on('change:x', function () { attr++; });
model.on('change', function () { main++; });
model.set({x:2}, {validate:true});
model.set({x:1}, {validate:true});
deepEqual([attr, main, error], [1, 1, 1]);
});
test("set triggers changes in the correct order", function() {
var value = null;
var model = new Backbone.Model;