Permalink
Browse files

fixing validation & internal change flag order

  • Loading branch information...
1 parent 61021d3 commit e12e1d8d45fdf51f72714da7678de930d72aa1e8 @tgriesser tgriesser committed Dec 27, 2012
Showing with 21 additions and 3 deletions.
  1. +3 −3 backbone.js
  2. +18 −0 test/model.js
View
@@ -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
@@ -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;

0 comments on commit e12e1d8

Please sign in to comment.