This repository has been archived by the owner on Jan 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExpandableRecordArray.js
55 lines (54 loc) · 1.71 KB
/
ExpandableRecordArray.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
define([
'ember-model'
], function (Ember) {
return Ember.RecordArray.extend({
isLoaded : false,
isLoading : false,
load : function (array) {
var observer, self;
this.set('isLoading', true);
self = this;
observer = function () {
var content;
if (this.get('isLoaded')) {
content = self.get('content');
array.removeObserver('isLoaded', observer);
array.forEach(function (record) {
if (!self.contains(record)) {
return self.pushObject(record);
}
});
self.set('isLoading', false);
return self.set('isLoaded', true);
}
};
return array.addObserver('isLoaded', observer);
},
observe : function (collection, filterWith) {
this.set('filterWith', filterWith);
return collection.addArrayObserver(this, {
willChange : 'observedArrayWillChange',
didChange : 'observedArraydidChange'
});
},
observedArrayWillChange : function () {
},
observedArraydidChange : function (array, index, removedCount, addedCount) {
var addedObjects, object, _i, _len, _results;
addedObjects = array.slice(index, index + addedCount);
_results = [];
for (_i = 0, _len = addedObjects.length; _i < _len; _i++) {
object = addedObjects[_i];
if (this.get('filterWith').call(this, object)) {
_results.push(this.pushObject(object));
} else {
_results.push(void 0);
}
}
return _results;
},
pushObject : function (record) {
return this.get('content').pushObject(record);
}
});
});