Skip to content

Commit

Permalink
initial implementation and test #1417
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisabril committed Jan 30, 2015
1 parent d0c4338 commit 9c17da1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
17 changes: 12 additions & 5 deletions map/backup/backup.js
@@ -1,5 +1,5 @@
//allows you to backup and restore a map instance
steal('can/util', 'can/map', 'can/util/object', function (can) {
steal('can/util', 'can/compute', 'can/map', 'can/util/object', function (can) {
var flatProps = function (a, cur) {
var obj = {};
for (var prop in a) {
Expand All @@ -11,19 +11,26 @@ steal('can/util', 'can/map', 'can/util/object', function (can) {
}
return obj;
};

var oldSetup = can.Map.prototype.setup;

can.extend(can.Map.prototype, {
setup: function() {
this._backupStore = can.compute();
return oldSetup.apply(this, arguments);
},

backup: function () {
this._backupStore = this._attrs();
this._backupStore(this.attr());
return this;
},
isDirty: function (checkAssociations) {
return this._backupStore && !can.Object.same(this._attrs(), this._backupStore, undefined, undefined, undefined, !! checkAssociations);
return this._backupStore() && !can.Object.same(this.attr(), this._backupStore(), undefined, undefined, undefined, !! checkAssociations);
},
restore: function (restoreAssociations) {
var props = restoreAssociations ? this._backupStore : flatProps(this._backupStore, this);
var props = restoreAssociations ? this._backupStore() : flatProps(this._backupStore(), this);
if (this.isDirty(restoreAssociations)) {
this._attrs(props, true);
this.attr(props, true);
}
return this;
}
Expand Down
21 changes: 21 additions & 0 deletions map/backup/backup_test.js
Expand Up @@ -94,4 +94,25 @@ steal("can/map/backup", "can/model", "can/test", function () {
map.restore();
ok(!map.attr('foo'), 'there is no foo property');
});

test('isDirty wrapped in a compute should trigger changes #1417', function() {
expect(2);
var recipe = new Recipe({
name: 'bread'
});

recipe.backup();

var c = can.compute(function() {
return recipe.isDirty();
});

ok(!c(), 'isDirty is false');

c.bind('change', function() {
ok(c(), 'isDirty is true and a change has occurred');
});

recipe.attr('name', 'cheese');
});
});

0 comments on commit 9c17da1

Please sign in to comment.