-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (68 loc) · 2.53 KB
/
index.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
/**
*
*
* DataMember is a class to help you to track on plenty changes.
*
* constructor :
*
* Parameters:
* * self - this of your environment where you listen to the changes
* * dataToMember - List of names of the data to track
*
* Public Methods :
*
* startRemember : start to remmber current data from this time.
*
* isChanged : return true or false if some data changes from the last time you run startRemember.
*
* whoChanged : return a list of all data that changed.
*
*
**/
define(['underscore'], function ( _) {
DataMember = function (self, dataToMember) {
/** Private Fields **/
this.dataToMember = dataToMember;
this.rememberedData = {};
/** Private Methods **/
this.fieldChanged = function (newValue) {
if(newValue.changed !== this.rememberedData["changed"]) {
this.rememberedData["isChanged"] = true;
}
this.rememberedData["isChanged"] = false;
};
this.trimAllFieldsInObjectAndChildren = function(o) {
return JSON.parse(JSON.stringify(o).replace(/"\s+|\s+"/g, '"'));
};
/** Public Methods **/
this.startRemember = function () {
// Remember current values and listen to them
_.each(this.dataToMember, function (field) {
this.rememberedData["changed"] = {};
this.rememberedData.changed[field] = self.get(field);
this.rememberedData["isChanged"] = false;
self.on('change:' + field, this.fieldChanged.bind(this));
}.bind(this));
};
this.isChanged = function () {
var changed = false;
_.each(this.dataToMember, function (field) {
if(field.changed) {
changed = true;
return;
}
}.bind(this));
return changed;
};
this.whoChanged = function () {
var changedList = [];
_.each(this.dataToMember, function (field) {
if(field.changed) {
changedList.push(field);
}
}.bind(this));
return changedList;
};
};
return DataMember;
});