diff --git a/map/backup/backup.js b/map/backup/backup.js index 48ec0329169..0e83768f29a 100644 --- a/map/backup/backup.js +++ b/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) { @@ -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; } diff --git a/map/backup/backup_test.js b/map/backup/backup_test.js index 7353a9ceb57..a973a8de028 100644 --- a/map/backup/backup_test.js +++ b/map/backup/backup_test.js @@ -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'); + }); });