-
Notifications
You must be signed in to change notification settings - Fork 3
/
meteor-collection.html
125 lines (118 loc) · 2.77 KB
/
meteor-collection.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
<!--
A wrapper to meteor Mongo.Collection object.
-->
<script>
Polymer({
is: 'meteor-collection',
/**
* Fired when a document has been inserted. detail contains the id of the new document.
*
* @event insert
*/
/**
* Fired when a documents has been removed.
*
* @event removed
*/
/**
* Fired when a documents has been updated.
*
* @event updated
*/
/**
* Fired when an error occurs while inserting or removing document. detail contains the error object
*
* @event error
*/
properties: {
/**
* Name of the mongo collection.
*/
name: {
type: String,
value: null,
observer: '_nameChanged'
},
/**
* The collection instance.
*/
_ref: {
type: Object,
value: null,
notify: true,
}
},
/**
* React to collection name changes.
*/
_nameChanged: function() {
if (this.name) {
if (window['__meteor_elements_'+this.name]) {
this._ref = window['__meteor_elements_'+this.name];
} else {
this._ref = new Mongo.Collection(this.name);
window['__meteor_elements_'+this.name] = this._ref;
}
} else {
this._ref = null;
}
this._setupChildQueries();
},
/**
* Setup the collection reference to childs elements.
*/
_setupChildQueries: function() {
var childs = Polymer.dom(this).childNodes;
for(var i=0; i<childs.length; i++) {
if (typeof childs[i]._setupCollection === 'function') {
childs[i]._setupCollection(this._ref);
}
}
},
/**
* Insert a document to collection.
*
* @method insert
*/
insert: function(doc) {
if (this._ref)
this._ref.insert(doc, function(err, id) {
if (err)
this.fire('error', { error: err });
else
this.fire('insert', { id: id });
}.bind(this));
},
/**
* Remove documents in collection.
*
* @method remove
*/
remove: function(selector) {
if (this._ref)
this._ref.remove(selector, function(err) {
if (err)
this.fire('error', { error: err });
else
this.fire('removed');
}.bind(this));
},
/**
* Modify one or more documents in the collection.
*
* @method update
*/
update: function(selector, modifier, options) {
options = options || {};
if (this._ref)
this._ref.update(selector, modifier, options, function(err) {
if (err)
this.fire('error', { error: err });
else
this.fire('updated');
}.bind(this));
},
ready: function() {
}
});
</script>