-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmeteor-query.html
153 lines (135 loc) · 4.15 KB
/
meteor-query.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!--
Start a reactive collection query. This element must be a direct child of a meteor-collection element.
Example :
<meteor-collection name="peoples" on-insert="_onInsert">
<meteor-query query='{ "age": 18 }' options='{"sort": { size: -1 } }' data="{{eighties}}"></meteor-query>
</meteor-collection>
<template is="dom-repeat" items="{{eighties}}">
<div>
<span>{{item.name}}</span>
</div>
</template>
-->
<script>
Polymer({
is: 'meteor-query',
properties: {
/**
* Data contains collection objects matching the query.
*/
data: {
type: Array,
value: function() { return []; },
notify: true,
readOnly: true
},
/**
* The collection object to query
*/
_collection: {
type: Object,
value: null
},
/**
* A query describing the documents to find.
*/
query: {
type: Object,
value: function() { return {}; },
},
/**
* An object containing query options (sort, skip and more).
*/
options: {
type: Object,
value: function() { return {}; },
},
/**
* Activate this query.
*/
enable: {
type: Boolean,
value: false,
},
},
observers: [ '_refind(_collection, query, options, enable)' ],
_setupCollection: function(collection) {
this._collection = collection;
},
_refind: function(_collection, query, options, enable) {
this.debounce("refind", this._setupObserver);
},
_setupObserver: function(_collection, query, options, enable) {
//console.log("(re)find meteor-query "+this._collection+" "+this.query+" "+this.options);
if (this._handle) {
this._handle.stop();
this._handle = null;
this.splice('data', 0, this.data.length);
}
if (this._collection && this.enable) {
this.initialdata = [];
this.initdone = false;
this._handle = this._collection.find(this.query, this.options).observeChanges({
addedBefore: function(id, fields, before) {
// added _id
fields._id = id;
if (before == null) {
if (this.initdone) {
this.push('data', fields);
} else {
this.initialdata.push(fields);
}
} else {
if (this.initdone) {
var i = this._findIndex(this.data, before);
this.splice('data', i, 0, fields);
} else {
var i = this._findIndex(this.initData, before);
this.initdata.splice(i, 0, fields);
}
}
}.bind(this),
changed: function(id, fields) {
var index = this._findIndex(this.data, id);
for(var field in fields) {
var path = ['data', index, field].join('.');
this.set(path, fields[field]);
}
}.bind(this),
movedBefore: function(id, before) {
var oldindex = this._findIndex(this.data, id);
var entry = this.splice('data', oldindex, 1)[0];
if (before) {
var newindex = this._findIndex(this.data, before);
this.splice('data', newindex, 0, entry);
} else {
this.push('data', entry);
}
}.bind(this),
removed: function(id) {
var index = this._findIndex(this.data, id);
this.splice('data', i, 1);
}.bind(this)
});
// push initial data as observeChanges returns.
this.initdone = true;
// unshift splice arguements to apply it to data array.
this.initialdata.unshift(0);
this.initialdata.unshift(0);
this.initialdata.unshift('data');
this.splice.apply(this, this.initialdata);
this.initialdata = null;
}
},
_findIndex: function(data, id) {
for (i = 0; i < data.length; i++) {
if (EJSON.equals(data[i]._id,id)) {
return i;
}
}
return -1;
},
ready: function() {
}
});
</script>