-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactive-data-array.js
More file actions
43 lines (36 loc) · 1.29 KB
/
Copy pathreactive-data-array.js
File metadata and controls
43 lines (36 loc) · 1.29 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
ReactiveDataArray = function (targetArray, cursor, callback) {
var startupCall = true;
// Get index of doc inside target array using the _id
var atIndex = function (id) {
return targetArray.findIndex(function (doc) {
return doc._id === id;
});
};
return cursor.observeChanges({
added: function (id, fields) {
var doc = Object.assign({_id: id}, fields);
targetArray.push(doc);
// Suppress callbacks on calls for existing docs
if (targetArray.length === cursor.count()) {
if (!callback) {return;}
callback({
type: "added",
startup: startupCall,
doc: doc
});
startupCall = false;
}
},
changed: function (id, fields) {
// Merge (overwrite) changed fields into target doc
var newDoc = Object.assign(targetArray[atIndex(id)], fields);
if (!callback) {return;}
callback({type: "changed", doc: newDoc});
},
removed: function (id) {
var oldDoc = targetArray.splice(atIndex(id), 1);
if (!callback) {return;}
callback({type: "removed", doc: oldDoc});
}
});
};