can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
49 lines (48 loc) • 1.47 kB
JavaScript
/*!
* CanJS - 2.3.34
* http://canjs.com/
* Copyright (c) 2018 Bitovi
* Mon, 30 Apr 2018 20:56:51 GMT
* Licensed MIT
*/
/*can@2.3.34#map/backup/backup*/
define([
'can/util/library',
'can/compute',
'can/map',
'can/util/object'
], function (can) {
var flatProps = function (a, cur) {
var obj = {};
for (var prop in a) {
if (typeof a[prop] !== 'object' || a[prop] === null || a[prop] instanceof Date) {
obj[prop] = a[prop];
} else {
obj[prop] = cur.attr(prop);
}
}
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.attr());
return this;
},
isDirty: function (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);
if (this.isDirty(restoreAssociations)) {
this.attr(props, true);
}
return this;
}
});
return can.Map;
});