-
Notifications
You must be signed in to change notification settings - Fork 142
/
firebase-database-behavior.html
148 lines (129 loc) · 3.78 KB
/
firebase-database-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
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../app-storage/app-storage-behavior.html">
<link rel="import" href="firebase-common-behavior.html">
<link rel="import" href="firebase-database-script.html">
<script>
(function() {
'use strict';
/** @polymerBehavior Polymer.FirebaseDatabaseBehavior */
Polymer.FirebaseDatabaseBehaviorImpl = {
properties: {
db: {
type: Object,
computed: '__computeDb(app)'
},
ref: {
type: Object,
computed: '__computeRef(db, path, disabled)',
observer: '__refChanged'
},
/**
* Path to a Firebase root or endpoint. N.B. `path` is case sensitive.
* @type {string|null}
*/
path: {
type: String,
value: null,
observer: '__pathChanged'
},
/**
* When true, Firebase listeners won't be activated. This can be useful
* in situations where elements are loaded into the DOM before they're
* ready to be activated (e.g. navigation, initialization scenarios).
*/
disabled: {
type: Boolean,
value: false
},
/**
* `exists` is set to `true` when the data actually exists for the
* specified path; `false` otherwise.
* When we are unable to determine whether data exists or not
* (e.g. first round trip to the server not yet performed) the value
* is `null`
*/
exists: {
type: Boolean,
notify: true,
value: null,
readOnly: true,
reflectToAttribute: true
},
},
observers: [
'__onlineChanged(online)'
],
/**
* Set the firebase value.
* @return {!firebase.Promise<void>}
*/
_setFirebaseValue: function(path, value) {
this._log('Setting Firebase value at', path, 'to', value)
var key = value && value.$key;
var leaf = value && value.hasOwnProperty('$val');
if (key) {
value.$key = null;
}
var result = this.db.ref(path).set(leaf ? value.$val : value);
if (key) {
value.$key = key;
}
return result;
},
__computeDb: function(app) {
return app ? app.database() : null;
},
__computeRef: function(db, path) {
if (db == null ||
path == null ||
!this.__pathReady(path) ||
this.disabled) {
return null;
}
return db.ref(path);
},
/**
* Override this method if needed.
* e.g. to detach or attach listeners.
*/
__refChanged: function(ref, oldRef){
return;
},
__pathChanged: function(path, oldPath) {
this._setExists(null);
if (!this.disabled && !this.valueIsEmpty(this.data)) {
this.syncToMemory(function() {
this.data = this.zeroValue;
this.__needSetData = true;
});
}
},
__pathReady: function(path) {
return path && path.split('/').slice(1).indexOf('') < 0;
},
__onlineChanged: function(online) {
if (!this.ref) {
return;
}
if (online) {
this.db.goOnline();
} else {
this.db.goOffline();
}
}
};
/** @polymerBehavior */
Polymer.FirebaseDatabaseBehavior = [
Polymer.AppStorageBehavior,
Polymer.FirebaseCommonBehavior,
Polymer.FirebaseDatabaseBehaviorImpl
];
})();
</script>