-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathObservableArray.js
More file actions
116 lines (104 loc) · 2.98 KB
/
ObservableArray.js
File metadata and controls
116 lines (104 loc) · 2.98 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
/**
* @class ObservableArray
* @fires _oa_update ObservableArray change event
* @author Stefano Sergio
*/
Refuel.define('ObservableArray',{inherits: 'Events'},
function ObservableArray() {
this.config = {};
var index = 0;
var data, unFilteredData, lastAppliedFilter;
this.init = function(myConfig) {
this.config = Refuel.mix(this.config, myConfig);
this.set(this.config.value);
['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'slice'].forEach(handleChange.bind(this));
}
function refreshLength() {
var e = {action: 'update', data: data.length, prop: 'length'};
this.notify('_oa_update',e);
}
function handleChange(methodName) {
this[methodName] = function () {
var r = data[methodName].apply(data, arguments);
switch(methodName) {
case 'push':
var index = data.length-1;
watchElement.call(this, index);
this.length = data.length;
var e = {action: 'add', index: index, data: this[index]};
this.notify('_oa_update',e);
break;
case 'splice':
var index = arguments[0];
this.length = data.length;
unWatchElement.call(this, index);
var e = {action: 'delete', index: index};
this.notify('_oa_update',e);
break;
}
return r;
};
}
this.__defineGetter__('data', function() {
return data;
});
//XXX Quando viene osservata questi get/set vengono sovrascritti da Observer
Object.defineProperty(this, 'length', {
configurable: true,
set: function(val) {
var e = {action: 'update', data: data.length, prop: 'length'};
this.notify('_oa_update',e);
},
get: function() {
return data.length;
}
});
function watchElement(index) {
if(!this.__lookupGetter__(index)) {
(function(context, thisIndex) {
Object.defineProperty(context, thisIndex, {
configurable: true,
set: function(val) {
context.setElementAt(thisIndex, val);
},
get: function() {
return context.getElementAt(thisIndex);
}
});
})(this, index);
}
}
function unWatchElement(index) {
if (index) delete this[index];
resetWatchers.call(this);
}
function resetWatchers() {
for (var i = 0; i < data.length; i++) {
delete this[i];
watchElement.call(this, i)
};
}
this.setElementAt = function(index, val) {
data[index] = val;
}
this.getElementAt = function(index) {
return data[index];
}
this.filter = function(callback) {
return data.filter(callback);
}
this.clear = function() {
for (var i = 0; i < data.length; i++) {
delete this[i];
}
data = [];
data.length = 0;
this.length = 0;
}
this.set = function() {
data = this.config.value;
this.length = data.length;
resetWatchers.call(this);
this.notify('_oa_update',{action: 'set'});
}
});