-
Notifications
You must be signed in to change notification settings - Fork 72
/
firebase-query-behavior.html
209 lines (177 loc) · 5.92 KB
/
firebase-query-behavior.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="firebase.html">
<script>
/**
* **Note: This element is for the older Firebase 2 API**
* For the latest official Firebase 3.0-compatible component from the Firebase team,
* see the [polymerfire](https://github.com/firebase/polymerfire) component.
*
* @polymerBehavior
*/
Polymer.FirebaseQueryBehavior = {
properties: {
/**
* A Firebase API location that references an accessible document.
*/
location: {
type: String,
notify: true,
reflectToAttribute: true
},
/**
* Firebase Query object corresponding to `location`.
*/
query: {
type: Object,
notify: true,
readOnly: true
},
/**
* If true, verbose debugging information will be printed to the console.
*/
log: {
type: Boolean,
value: false,
reflectToAttribute: true
}
},
observers: [
'_dataChanged(data.*)'
],
created: function() {
this._receivingLocalChanges = false;
this._receivingRemoteChanges = false;
},
get dataAsObject() {
if (Array.isArray(this.data)) {
return this.data.reduce(function(object, value, index) {
object[index] = value;
}, {});
}
return this.data;
},
/**
* Disconnects the current Firebase Query instance.
*/
disconnect: function() {
this.location = '';
},
_localDataChanged: function(changes) {
// Virtual..
},
_applyLocalDataChange: function(applyChange) {
this._receivingLocalChanges = true;
applyChange.call(this);
this._receivingLocalChanges = false;
},
_applyRemoteDataChange: function(applyChange) {
this._receivingRemoteChanges = true;
applyChange.call(this);
this._receivingRemoteChanges = false;
},
_dataChanged: function(changes) {
if (this._receivingRemoteChanges ||
this._receivingLocalChanges) {
return;
}
this._localDataChanged(changes);
},
_queryChanged: function(query, oldQuery) {
if (oldQuery) {
this._stopListeningToQuery(oldQuery);
}
if (query) {
this._listenToQuery(query);
}
},
_listenToQuery: function(query) {
this._log('Adding Firebase event handlers.');
query.on('value', this._onQueryValue, this._onQueryCancel, this);
query.on('child_added', this._onQueryChildAdded, this._onQueryCancel, this);
query.on('child_removed', this._onQueryChildRemoved, this._onQueryCancel, this);
query.on('child_changed', this._onQueryChildChanged, this._onQueryCancel, this);
query.on('child_moved', this._onQueryChildMoved, this._onQueryCancel, this);
},
_stopListeningToQuery: function(query) {
this._log('Removing Firebase event handlers');
query.off('value', this._onQueryValue, this);
query.off('child_added', this._onQueryChildAdded, this);
query.off('child_removed', this._onQueryChildRemoved, this);
query.off('child_changed', this._onQueryChildChanged, this);
query.off('child_moved', this._onQueryChildMoved, this);
},
_onQueryValue: function(snapshot) {
this._log('Firebase Event: "value"', snapshot);
this.fire('firebase-value', snapshot, { bubbles: false });
},
_onQueryChildAdded: function(childSnapshot, previousChildName) {
this._log('Firebase Event: "child_added"', childSnapshot, previousChildName);
this.fire('firebase-child-added', {
childSnapshot: childSnapshot,
previousChildName: previousChildName
}, { bubbles: false });
},
_onQueryChildRemoved: function(oldChildSnapshot) {
this._log('Firebase Event: "child_removed"', oldChildSnapshot);
this.fire('firebase-child-removed', {
oldChildSnapshot: oldChildSnapshot
}, { bubbles: false });
},
_onQueryChildChanged: function(childSnapshot, previousChildName) {
this._log('Firebase Event: "child_changed"', childSnapshot, previousChildName);
this.fire('firebase-child-changed', {
childSnapshot: childSnapshot,
previousChildName: previousChildName
}, { bubbles: false });
},
_onQueryChildMoved: function(childSnapshot, previousChildName) {
this._log('Firebase Event: "child_moved"', childSnapshot, previousChildName);
this.fire('firebase-child-moved', {
childSnapshot: childSnapshot,
previousChildName: previousChildName
}, { bubbles: false });
},
_onQueryCancel: function(error) {
if (error) {
this._error('Firebase Error', error);
}
this._log('Firebase query subscription cancelled.');
this.disconnect();
},
_fireQueryError: function(error) {
this.fire('firebase-query-error', error);
this._error(error);
},
_log: function() {
var args;
if (this.log) {
args = Array.prototype.slice.call(arguments).map(function(arg) {
if (arg && typeof arg.val === 'function') {
return arg.val();
}
return arg;
});
console.log.apply(console, args);
}
},
_warn: function() {
if (this.log) {
Polymer.Base._warn(this._log.apply(this, arguments));
}
},
_error: function() {
if (this.log) {
Polymer.Base._error(this._log.apply(this, arguments));
}
}
};
</script>