-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfilter-collections-server.js
More file actions
89 lines (60 loc) · 2.15 KB
/
filter-collections-server.js
File metadata and controls
89 lines (60 loc) · 2.15 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
FilterCollections = {};
FilterCollections.publish = function (collection, options) {
var self = this;
options = options || {};
var callbacks = options.callbacks || {};
// var cursor = {};
var name = (options.name) ? options.name : collection._name;
var publisherResultsId = 'fc-' + name + '-results';
var publisherCountId = 'fc-' + name + '-count';
var publisherCountCollectionName = name + 'CountFC';
/**
* Publish query results.
*/
Meteor.publish(publisherResultsId, function (query) {
var allow = true;
if (callbacks.allow && _.isFunction(callbacks.allow))
allow = callbacks.allow(query, this);
if(!allow){
throw new Meteor.Error(417, 'Not allowed');
}
query = (query && !_.isEmpty(query)) ? query : {};
query.selector = query.selector || {};
query.options = query.options || {
sort: [],
skip: 0,
limit: 10
};
if (callbacks.beforePublish && _.isFunction(callbacks.beforePublish))
query = callbacks.beforePublish(query, this) || query;
var cursor = collection.find(query.selector, query.options);
if (callbacks.afterPublish && _.isFunction(callbacks.afterPublish))
cursor = callbacks.afterPublish('results', cursor, this) || cursor;
return cursor;
});
/**
* Publish result count.
*/
Meteor.publish(publisherCountId, function (query) {
var self = this;
var allow = true;
var cursor = {};
if (callbacks.allow && _.isFunction(callbacks.allow))
allow = callbacks.allow(query, this);
if(!allow){
throw new Meteor.Error(417, 'Not allowed');
}
query = (query && !_.isEmpty(query)) ? query : {};
query.selector = query.selector || {};
if(callbacks.beforePublish && _.isFunction(callbacks.beforePublish))
query = callbacks.beforePublish(query, this) || query;
count = collection.find(query.selector).count() || 0;
if(callbacks.afterPublish && _.isFunction(callbacks.afterPublish))
cursor = callbacks.afterPublish('count', cursor, this) || cursor;
self.added(publisherCountCollectionName, Meteor.uuid(), {
count: count,
query: query
});
this.ready();
});
};