-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStore.js
More file actions
230 lines (191 loc) · 7.03 KB
/
DataStore.js
File metadata and controls
230 lines (191 loc) · 7.03 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** DataStore
//*****************************************************************************/
/**
* Simple data store used to manage records and broadcast updates
*
******************************************************************************/
javaxt.dhtml.DataStore = function(data) {
var me = this;
var listeners = {};
var records = [];
this.length = 0;
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (isArray(data)){
records = data;
me.length = records.length;
}
};
//**************************************************************************
//** addEventListener
//**************************************************************************
this.addEventListener = function(name, fn, scope){
if (!fn) return;
var listener = {
fn: fn,
scope: scope
};
if (listeners[name]){
listeners[name].push(listener);
}
else{
listeners[name]=[listener];
}
};
var fireEvent = function(name, ret){
if (listeners[name]){
var arr = listeners[name];
for (var i=0; i<arr.length; i++){
var listener = arr[i];
listener.fn.apply(listener.scope, isArray(ret) ? ret : [ret]);
}
}
};
//**************************************************************************
//** push
//**************************************************************************
this.push = function(record){
me.add(record);
};
//**************************************************************************
//** add
//**************************************************************************
this.add = function(record){
records.push(record);
me.length = records.length;
fireEvent("add", record);
};
//**************************************************************************
//** insert
//**************************************************************************
/** Used to add a record at a specific index and pushes records below it
* down a level
*/
this.insert = function(record, idx){
if (!isNaN(idx)){
records.splice(idx, 0, record);
}
else{
records.push(record);
}
me.length = records.length;
fireEvent("add", record);
};
//**************************************************************************
//** pop
//**************************************************************************
/** Removes and returns the last element from the data store
*/
this.pop = function(){
var obj = records.pop();
me.length = records.length;
fireEvent("remove", obj);
};
//**************************************************************************
//** shift
//**************************************************************************
/** Removes and returns the first element from the data store
*/
this.shift = function(){
var obj = records.shift();
me.length = records.length;
fireEvent("remove", obj);
};
//**************************************************************************
//** remove
//**************************************************************************
/** Used to remove a record from the data store
*/
this.remove = function(record){
for (var i=0; i<records.length; i++){
if (records[i]===record){
me.removeAt(i);
break;
}
}
};
//**************************************************************************
//** removeAt
//**************************************************************************
this.removeAt = function(idx){
me.splice(idx, 1);
};
//**************************************************************************
//** clear
//**************************************************************************
this.clear = function(){
me.splice(0,records.length);
};
//**************************************************************************
//** splice
//**************************************************************************
this.splice = function(idx, numRecords){
var arr = records.splice(idx, numRecords);
me.length = records.length;
fireEvent("remove", arr);
};
//**************************************************************************
//** get
//**************************************************************************
this.get = function(idx){
return records[idx];
};
//**************************************************************************
//** set
//**************************************************************************
this.set = function(idx, record){
var orgRecord = records[idx];
records[idx] = record;
fireEvent("update", [record, orgRecord]);
};
//**************************************************************************
//** sort
//**************************************************************************
this.sort = function(fn){
records.sort(fn);
};
//**************************************************************************
//** forEach
//**************************************************************************
/** Used to iterate through all the records in the store using a callback
* function. Example:
<pre>
store.forEach(function (record, i) {
console.log(i, record);
});
</pre>
* Optional: return true in the callback function if you wish to break and
* iterate stop iterating.
*/
this.forEach = function(callback){
if (!callback) return;
for (var i=0; i<records.length; i++){
var r = callback.apply(me, [me.get(i), i]);
if (r===true) return;
}
};
//**************************************************************************
//** destroy
//**************************************************************************
this.destroy = function(){
records.splice(0,records.length);
me.length = 0;
for (var key in listeners) {
if (listeners.hasOwnProperty(key)){
var arr = listeners[key];
arr = arr.splice(0,arr.length);
for (var i=0; i<arr.length; i++){
arr[i] = null;
}
delete listeners[key];
}
}
};
var isArray = javaxt.dhtml.utils.isArray;
init();
};