| 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 <link rel="import" href="firebase-query-behavior.html"> |
| 14 |
| 15 <!-- |
| 16 An element wrapper for the Firebase API. |
| 17 |
| 18 A `<firebase-document>` is a reference to a remote document somewhere on |
| 19 Firebase. The element fetchs a document at a provided `location`, and exposes |
| 20 it as an Object that is suitable for deep two-way databinding. |
| 21 |
| 22 Example: |
| 23 |
| 24 <firebase-document |
| 25 location="https://dinosaur-facts.firebaseio.com/dinosaurs" |
| 26 data="{{dinosaurs}}"> |
| 27 |
| 28 In the above example, if the `dinosaurs` object is data-bound elsewhere via |
| 29 Polymer's data-binding system, changes to the document will be automatically |
| 30 reflected in the remote document and any other clients referencing that |
| 31 document. |
| 32 --> |
| 33 |
| 34 <script> |
| 35 Polymer({ |
| 36 is: 'firebase-document', |
| 37 |
| 38 behaviors: [ |
| 39 Polymer.FirebaseQueryBehavior |
| 40 ], |
| 41 |
| 42 properties: { |
| 43 |
| 44 /** |
| 45 * Firebase Query object corresponding to `location`. |
| 46 */ |
| 47 query: { |
| 48 type: Object, |
| 49 notify: true, |
| 50 computed: '_computeQuery(location)', |
| 51 observer: '_queryChanged' |
| 52 }, |
| 53 |
| 54 /** |
| 55 * The `data` object mapped to `location`. |
| 56 */ |
| 57 data: { |
| 58 type: Object, |
| 59 notify: true |
| 60 } |
| 61 }, |
| 62 |
| 63 listeners: { |
| 64 'firebase-value': '_onFirebaseValue' |
| 65 }, |
| 66 |
| 67 _applyLocalDataChanges: function(change) { |
| 68 var pathFragments = change.path.split('.'); |
| 69 |
| 70 if (pathFragments.length === 1) { |
| 71 this._updateRemoteDocument(); |
| 72 return; |
| 73 } |
| 74 |
| 75 this._setRemoteDocumentChild( |
| 76 pathFragments[1], |
| 77 change.base[pathFragments[1]] |
| 78 ); |
| 79 }, |
| 80 |
| 81 _onFirebaseValue: function(event) { |
| 82 this._applyRemoteDataChange(function() { |
| 83 this.set('data', event.detail.val()); |
| 84 }); |
| 85 }, |
| 86 |
| 87 _computeQuery: function(location) { |
| 88 if (!location) { |
| 89 return; |
| 90 } |
| 91 |
| 92 return new Firebase(location); |
| 93 }, |
| 94 |
| 95 _updateRemoteDocument: function() { |
| 96 this._log('Updating remote document'); |
| 97 this.query.update(this.dataAsObject); |
| 98 }, |
| 99 |
| 100 _setRemoteDocumentChild: function(key, value) { |
| 101 this._log('Setting child "' + key + '" to', value); |
| 102 this.query.child(key).set(value); |
| 103 }, |
| 104 |
| 105 _removeRemoteDocumentChild: function(key) { |
| 106 this._log('Removing child "' + key + '"'); |
| 107 this.query.child(key).remove(); |
| 108 } |
| 109 }); |
| 110 </script> |
| OLD | NEW |