hasChanged shouldn't be true after the constructor.

This commit is contained in:
Jeremy Ashkenas
2012-12-21 10:51:02 -05:00
parent 3ffcce8cb8
commit 2eab0fe3b8
2 changed files with 17 additions and 6 deletions

View File

@@ -235,7 +235,6 @@
var defaults;
var attrs = attributes || {};
this.cid = _.uniqueId('c');
this.changed = {};
this.attributes = {};
if (options && options.collection) this.collection = options.collection;
if (options && options.parse) attrs = this.parse(attrs, options) || {};
@@ -243,6 +242,7 @@
attrs = _.defaults({}, attrs, defaults);
}
this.set(attrs, options);
this.changed = {};
this.initialize.apply(this, arguments);
};
@@ -354,7 +354,6 @@
this.trigger('change', this, options);
}
}
// this.changed = {};
this._pending = false;
this._changing = false;
return this;

View File

@@ -309,7 +309,7 @@ $(document).ready(function() {
test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, function() {
var model = new Backbone.Model({name: "Tim", age: 10});
deepEqual(model.changedAttributes(), {name: "Tim", age: 10});
deepEqual(model.changedAttributes(), false);
model.on('change', function() {
ok(model.hasChanged('name'), 'name changed');
ok(!model.hasChanged('age'), 'age did not');
@@ -317,15 +317,15 @@ $(document).ready(function() {
equal(model.previous('name'), 'Tim');
ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
});
equal(model.hasChanged(), true);
equal(model.hasChanged(undefined), true);
equal(model.hasChanged(), false);
equal(model.hasChanged(undefined), false);
model.set({name : 'Rob'});
equal(model.get('name'), 'Rob');
});
test("changedAttributes", 3, function() {
var model = new Backbone.Model({a: 'a', b: 'b'});
deepEqual(model.changedAttributes(), {a: 'a', b: 'b'});
deepEqual(model.changedAttributes(), false);
equal(model.changedAttributes({a: 'a'}), false);
equal(model.changedAttributes({a: 'b'}).a, 'b');
});
@@ -610,6 +610,18 @@ $(document).ready(function() {
equal(model.hasChanged('x'), true);
});
test("hasChanged gets cleared on the following set", 4, function() {
var model = new Backbone.Model;
model.set({x: 1});
ok(model.hasChanged());
model.set({x: 1});
ok(!model.hasChanged());
model.set({x: 2});
ok(model.hasChanged());
model.set({});
ok(!model.hasChanged());
});
test("save with `wait` succeeds without `validate`", 1, function() {
var model = new Backbone.Model();
model.url = '/test';