-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcan-define-backup.js
94 lines (84 loc) · 2.3 KB
/
can-define-backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
//allows you to backup and restore a map instance
var canReflect = require('can-reflect');
var SimpleObservable = require('can-simple-observable');
var diffDeep = require("can-diff/deep/deep");
var diffMap = require("can-diff/map/map");
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[prop];//cur.attr(prop);
}
}
return obj;
};
var assignNonEnumerable = function(base, props) {
for(var prop in props) {
Object.defineProperty(base, prop, {
enumerable: false,
configurable: true,
writable: true,
value: props[prop]
});
}
};
var observables = new WeakMap();
function getBackup(map) {
var obs = observables.get(map);
if(!obs) {
obs = new SimpleObservable();
observables.set(map, obs);
}
return obs;
}
function defineBackup(Map) {
assignNonEnumerable(Map.prototype, {
backup: function () {
var store = getBackup(this);
canReflect.setValue(store, this.serialize());
return this;
},
isDirty: function (checkAssociations) {
var store = getBackup(this);
var backupStore = canReflect.getValue(store);
if(!backupStore){
return false;
}
var currentValue = this.serialize();
var patches;
if(!! checkAssociations) {
patches = diffDeep(currentValue, backupStore);
} else {
patches = diffMap(currentValue, backupStore).filter(function(patch){
// only keep those that are not a set of deep object
if(patch.type !== "set") {
return true;
} else {
// check values .. if both objects ... we are not dirty ...
var curVal = currentValue[patch.key],
backupVal = backupStore[patch.key];
var twoObjectsCompared = curVal && backupVal && typeof curVal === "object" && typeof backupVal === "object";
return !twoObjectsCompared;
}
});
}
return patches.length;
},
restore: function (restoreAssociations) {
var store = getBackup(this);
var curVal = canReflect.getValue(store);
var props = restoreAssociations ? curVal : flatProps(curVal, this);
if (this.isDirty(restoreAssociations)) {
for(var prop in props) {
this[prop] = props[prop];
}
}
return this;
}
});
return Map;
}
module.exports = exports = defineBackup;