-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection-items.js
121 lines (118 loc) · 3.57 KB
/
collection-items.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
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
BaseCollection = function(name) {
this.name = name;
this.relations = {};
this.items = {};
}
BaseCollection.prototype.smartAdded = function(dependencyCursorId, id, fields) {
var items = this.items;
if (!items[id]) {
items[id] = new CollectionItem(id, this, fields, dependencyCursorId);
items[id].updateChildren(this.relations);
this.publication.added(this.name, id, fields);
} else {
var itemm = items[id];
_.each(fields, function(val, key) {
itemm.data[key] = itemm.data[key] || {};
itemm.data[key][dependencyCursorId] = deepCopy(val);
});
itemm.count++;
itemm.updateFromData(fields);
itemm.updateChildren(fields);
}
}
BaseCollection.prototype.smartChanged = function(dependencyCursorId, id, fields) {
var itemm = this.items[id];
var data = itemm.data;
_.each(fields, function(val, key) {
data[key] = data[key] || {};
if (val === undefined) {
delete data[key][dependencyCursorId];
} else {
data[key][dependencyCursorId] = deepCopy(val);
}
});
itemm.updateFromData(fields);
itemm.updateChildren(fields);
}
BaseCollection.prototype.smartRemoved = function(dependencyCursorId, id) {
var itemm = this.items[id];
if (!itemm) {
throw new Meteor.Error("Removing unexisting element '" + id + "' from collection '" + this.name + "'");
}
if (!--itemm.count) { // If reference counter was decremented to zero
this.publication.removed(this.name, id);
itemm.updateChildren(this.relations, true);
delete this.items[id];
} else {
var fields = {};
_.each(itemm.data, function(vals, key) {
if (dependencyCursorId in vals) {
fields[key] = 1;
delete vals[dependencyCursorId];
}
});
itemm.updateFromData(fields);
itemm.updateChildren(fields);
}
}
CollectionItem = function(id, collection, fields, dependencyCursorId) {
this.id = id;
this.collection = collection;
this.count = 1;
this.data = {};
this.mergedData = deepCopy(fields);
this.children = {};
var self = this;
_.each(fields, function(val, key) {
self.data[key] = self.data[key] || {};
self.data[key][dependencyCursorId] = deepCopy(val);
});
}
CollectionItem.prototype.updateFromData = function(fields) {
var res = {};
var merged = this.mergedData;
var self = this;
for (var key in fields) {
var cur = undefined;
_.each(self.data[key], (function(value) {
cur = _.extend(deepCopy(value), cur);
}));
res[key] = cur;
if (_.isUndefined(cur)) {
delete merged[key];
} else {
merged[key] = cur;
}
};
this.collection.publication.changed(this.collection.name, this.id, res);
}
CollectionItem.prototype.updateChildren = function(fields, removeAll) {
var self = this;
var update = {};
for (var key in fields) {
_.each(self.collection.relations[key], function(dep) {
update[dep.id] = dep;
});
};
_.each(update, function(dep) {
var toRemove = [];
if (dep.id in self.children) {
self.children[dep.id].forEach(function(x) {
toRemove = toRemove.concat(x.getActiveItems());
x.stop();
});
}
if (!removeAll) {
var context = new CallbacksWrapper(self.collection.publication.getCollectionByName);
var cursors = dep.callback.apply(context, [self.mergedData]);
var wrappers = getWrappersFromCallbackResult(self.collection.publication.getCollectionByName, cursors);
wrappers.push(context);
self.children[dep.id] = wrappers;
} else {
delete self.children[dep.id];
}
toRemove.forEach(function(args) {
args[0].smartRemoved(args[1], args[2]);
});
});
}