/
cursor_methods.js
119 lines (107 loc) · 2.93 KB
/
cursor_methods.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
CursorMethods = function (cursor, options) {
this.cursor = cursor;
for (var prop in options) {
if(options.hasOwnProperty(prop))
this[prop] = options[prop];
}
};
_.extend(CursorMethods.prototype, {
observe: function (callbacks) {
return this.handler.add(this.cursor.observe(callbacks));
},
observeChanges: function (callbacks) {
return this.handler.add(this.cursor.observeChanges(callbacks));
},
// adds a new cursor in a different collection to the main
publish: function (callback) {
var sub = this.sub;
var collection = this.collection;
if (callback) {
var stop = publish.prototype.relations(sub, {
cursor: this.cursor,
name: collection
}, callback);
return this.handler.add(stop);
} else {
// basic cursor
return this.observeChanges({
added: function (id, doc) {
sub.added(collection, id, doc);
},
changed: function (id, doc) {
sub.changed(collection, id, doc);
},
removed: function (id) {
sub.removed(collection, id);
}
});
}
},
// designed to change something in the master document while the callbacks are executed
// changes to the document are sent to the main document with the return of the callbacks
changeParentDoc: function (callbacks, onRemoved) {
var sub = this.sub,
_id = this._id,
collection = this.parentCollection,
result = {};
if (typeof callbacks == 'function') {
callbacks = {
added: callbacks,
changed: callbacks,
removed: onRemoved
}
}
this.observeChanges({
added: function (id, doc) {
result = callbacks.added && callbacks.added(id, doc);
},
changed: function (id, doc) {
var changes = callbacks.changed && callbacks.changed(id, doc);
if(changes)
sub.changed(collection, _id, changes);
},
removed: function (id) {
var changes = callbacks.removed && callbacks.removed(id);
if(changes)
sub.changed(collection, _id, changes);
}
});
return result;
},
// I'm thinking of deleting this method, I do not find great usability
group: function (make, field, options) {
var sub = this.sub,
_id = this._id,
collection = this.parentCollection,
result = [];
if(options) {
var sort = options.sort,
sortField = options.sortField;
}
this.observe({
addedAt: function (doc, atIndex) {
if(sort) {
atIndex = sort.indexOf(doc[sortField || '_id']);
result[atIndex] = make(doc, atIndex);
} else
result.push(make(doc, atIndex));
},
changedAt: function (doc, oldDoc, atIndex) {
if(sort)
atIndex = sort.indexOf(doc[sortField || '_id']);
var changes = make(doc, atIndex, oldDoc),
changesObj = {};
result[atIndex] = changes;
changesObj[field] = result;
// console.log(collection, _id);
sub.changed(collection, _id, changesObj);
},
removedAt: function (oldDoc, atIndex) {
var cb = options.onRemoved;
if(cb)
sub.changed(collection, _id, cb(oldDoc, atIndex));
}
});
return result;
}
});