Skip to content

Commit

Permalink
... progress ...
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 19, 2012
1 parent e92c526 commit cc8a5c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
1 change: 1 addition & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
this.trigger('change', this, options);
}

this.changed = {};
this._changing = false;
return this;
},
Expand Down
117 changes: 57 additions & 60 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ $(document).ready(function() {

test("unset and changedAttributes", 1, function() {
var model = new Backbone.Model({a: 1});
model.on('change', function() {
ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
});
model.unset('a');
var changedAttributes = model.changedAttributes();
ok('a' in changedAttributes, 'changedAttributes should contain unset properties');
});

test("using a non-default id attribute.", 5, function() {
Expand All @@ -260,67 +261,63 @@ $(document).ready(function() {
equal(model.isNew(), true);
});

// test("set an empty string", 1, function() {
// var model = new Backbone.Model({name : "Model"});
// model.set({name : ''});
// equal(model.get('name'), '');
// });
test("set an empty string", 1, function() {
var model = new Backbone.Model({name : "Model"});
model.set({name : ''});
equal(model.get('name'), '');
});

// test("clear", 3, function() {
// var changed;
// var model = new Backbone.Model({id: 1, name : "Model"});
// model.on("change:name", function(){ changed = true; });
// model.on("change", function() {
// var changedAttrs = model.changedAttributes();
// ok('name' in changedAttrs);
// });
// model.clear();
// equal(changed, true);
// equal(model.get('name'), undefined);
// });
test("clear", 3, function() {
var changed;
var model = new Backbone.Model({id: 1, name : "Model"});
model.on("change:name", function(){ changed = true; });
model.on("change", function() {
var changedAttrs = model.changedAttributes();
ok('name' in changedAttrs);
});
model.clear();
equal(changed, true);
equal(model.get('name'), undefined);
});

// test("defaults", 4, function() {
// var Defaulted = Backbone.Model.extend({
// defaults: {
// "one": 1,
// "two": 2
// }
// });
// var model = new Defaulted({two: null});
// equal(model.get('one'), 1);
// equal(model.get('two'), 2);
// Defaulted = Backbone.Model.extend({
// defaults: function() {
// return {
// "one": 3,
// "two": 4
// };
// }
// });
// model = new Defaulted({two: null});
// equal(model.get('one'), 3);
// equal(model.get('two'), 4);
// });
test("defaults", 4, function() {
var Defaulted = Backbone.Model.extend({
defaults: {
"one": 1,
"two": 2
}
});
var model = new Defaulted({two: null});
equal(model.get('one'), 1);
equal(model.get('two'), 2);
Defaulted = Backbone.Model.extend({
defaults: function() {
return {
"one": 3,
"two": 4
};
}
});
model = new Defaulted({two: null});
equal(model.get('one'), 3);
equal(model.get('two'), 4);
});

// test("change, hasChanged, changedAttributes, previous, previousAttributes", 12, function() {
// var model = new Backbone.Model({name : "Tim", age : 10});
// equal(model.changedAttributes(), false);
// model.on('change', function() {
// ok(model.hasChanged('name'), 'name changed');
// ok(!model.hasChanged('age'), 'age did not');
// ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
// equal(model.previous('name'), 'Tim');
// ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
// });
// equal(model.hasChanged(), false);
// equal(model.hasChanged(undefined), false);
// model.set({name : 'Rob'}, {silent : true});
// equal(model.hasChanged(), true);
// equal(model.hasChanged(undefined), true);
// equal(model.hasChanged('name'), true);
// model.change();
// equal(model.get('name'), 'Rob');
// });
test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, function() {
var model = new Backbone.Model({name : "Tim", age : 10});
equal(model.changedAttributes(), false);
model.on('change', function() {
ok(model.hasChanged('name'), 'name changed');
ok(!model.hasChanged('age'), 'age did not');
ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
equal(model.previous('name'), 'Tim');
ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
});
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'});
Expand Down

0 comments on commit cc8a5c2

Please sign in to comment.