| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="firebase.html"> |
| 13 |
| 14 <script> |
| 15 /** @polymerBehavior */ |
| 16 Polymer.FirebaseQueryBehavior = { |
| 17 properties: { |
| 18 /** |
| 19 * A Firebase API location that references an accessible document. |
| 20 */ |
| 21 location: { |
| 22 type: String, |
| 23 notify: true, |
| 24 reflectToAttribute: true |
| 25 }, |
| 26 |
| 27 /** |
| 28 * Firebase Query object corresponding to `location`. |
| 29 */ |
| 30 query: { |
| 31 type: Object, |
| 32 notify: true, |
| 33 readOnly: true |
| 34 }, |
| 35 |
| 36 /** |
| 37 * If true, verbose debugging information will be printed to the console. |
| 38 */ |
| 39 log: { |
| 40 type: Boolean, |
| 41 value: false, |
| 42 reflectToAttribute: true |
| 43 }, |
| 44 |
| 45 _receivingRemoteChanges: { |
| 46 type: Boolean, |
| 47 value: false |
| 48 } |
| 49 }, |
| 50 |
| 51 observers: [ |
| 52 '_dataChanged(data.*)' |
| 53 ], |
| 54 |
| 55 get dataAsObject() { |
| 56 if (Array.isArray(this.data)) { |
| 57 return this.data.reduce(function(object, value, index) { |
| 58 object[index] = value; |
| 59 }, {}); |
| 60 } |
| 61 |
| 62 return this.data; |
| 63 }, |
| 64 |
| 65 /** |
| 66 * Disconnects the current Firebase Query instance. |
| 67 */ |
| 68 disconnect: function() { |
| 69 this.location = ''; |
| 70 }, |
| 71 |
| 72 _applyLocalDataChanges: function(changes) { |
| 73 // Virtual.. |
| 74 }, |
| 75 |
| 76 _applyRemoteDataChange: function(applyChange) { |
| 77 this._receivingRemoteChanges = true; |
| 78 applyChange.call(this); |
| 79 this._receivingRemoteChanges = false; |
| 80 }, |
| 81 |
| 82 _dataChanged: function(changes) { |
| 83 if (this._receivingRemoteChanges) { |
| 84 return; |
| 85 } |
| 86 |
| 87 this._applyLocalDataChanges(changes); |
| 88 }, |
| 89 |
| 90 _queryChanged: function(query, oldQuery) { |
| 91 if (oldQuery) { |
| 92 this._stopListeningToQuery(oldQuery); |
| 93 } |
| 94 |
| 95 if (query) { |
| 96 this._listenToQuery(query); |
| 97 } |
| 98 }, |
| 99 |
| 100 _listenToQuery: function(query) { |
| 101 this._log('Adding Firebase event handlers.'); |
| 102 query.on('value', this._onQueryValue, this._onQueryCancel, this); |
| 103 query.on('child_added', this._onQueryChildAdded, this._onQueryCancel, this
); |
| 104 query.on('child_removed', this._onQueryChildRemoved, this._onQueryCancel,
this); |
| 105 query.on('child_changed', this._onQueryChildChanged, this._onQueryCancel,
this); |
| 106 query.on('child_moved', this._onQueryChildMoved, this._onQueryCancel, this
); |
| 107 }, |
| 108 |
| 109 _stopListeningToQuery: function(query) { |
| 110 this._log('Removing Firebase event handlers'); |
| 111 query.off('value', this._onQueryValue, this); |
| 112 query.off('child_added', this._onQueryChildAdded, this); |
| 113 query.off('child_removed', this._onQueryChildRemoved, this); |
| 114 query.off('child_changed', this._onQueryChildChanged, this); |
| 115 query.off('child_moved', this._onQueryChildMoved, this); |
| 116 }, |
| 117 |
| 118 _onQueryValue: function(snapshot) { |
| 119 this._log('Firebase Event: "value"', snapshot); |
| 120 this.fire('firebase-value', snapshot, { bubbles: false }); |
| 121 }, |
| 122 |
| 123 _onQueryChildAdded: function(childSnapshot, previousChildName) { |
| 124 this._log('Firebase Event: "child_added"', childSnapshot, previousChildNam
e); |
| 125 this.fire('firebase-child-added', { |
| 126 childSnapshot: childSnapshot, |
| 127 previousChildName: previousChildName |
| 128 }, { bubbles: false }); |
| 129 }, |
| 130 |
| 131 _onQueryChildRemoved: function(oldChildSnapshot) { |
| 132 this._log('Firebase Event: "child_removed"', oldChildSnapshot); |
| 133 this.fire('firebase-child-removed', { |
| 134 oldChildSnapshot: oldChildSnapshot |
| 135 }, { bubbles: false }); |
| 136 }, |
| 137 |
| 138 _onQueryChildChanged: function(childSnapshot, previousChildName) { |
| 139 this._log('Firebase Event: "child_changed"', childSnapshot, previousChildN
ame); |
| 140 this.fire('firebase-child-changed', { |
| 141 childSnapshot: childSnapshot, |
| 142 previousChildName: previousChildName |
| 143 }, { bubbles: false }); |
| 144 }, |
| 145 |
| 146 _onQueryChildMoved: function(childSnapshot, previousChildName) { |
| 147 this._log('Firebase Event: "child_changed"', childSnapshot, previousChildN
ame); |
| 148 this.fire('firebase-child-moved', { |
| 149 childSnapshot: childSnapshot, |
| 150 previousChildName: previousChildName |
| 151 }, { bubbles: false }); |
| 152 }, |
| 153 |
| 154 _onQueryCancel: function(error) { |
| 155 if (error) { |
| 156 this._error('Firebase Error', error); |
| 157 } |
| 158 |
| 159 this._log('Firebase query subscription cancelled.'); |
| 160 this.disconnect(); |
| 161 }, |
| 162 |
| 163 _log: function() { |
| 164 var args; |
| 165 |
| 166 if (this.log) { |
| 167 args = Array.prototype.slice.call(arguments).map(function(arg) { |
| 168 if (arg && typeof arg.val === 'function') { |
| 169 return arg.val(); |
| 170 } |
| 171 |
| 172 return arg; |
| 173 }); |
| 174 |
| 175 console.log.apply(console, args); |
| 176 } |
| 177 }, |
| 178 |
| 179 _error: function() { |
| 180 if (this.log) { |
| 181 console.error.apply(console, arguments); |
| 182 } |
| 183 } |
| 184 }; |
| 185 </script> |
| OLD | NEW |