forked from FirebaseExtended/polymerfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase-database-behavior.html
More file actions
101 lines (87 loc) · 2.29 KB
/
firebase-database-behavior.html
File metadata and controls
101 lines (87 loc) · 2.29 KB
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
<!--
@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.html">
<script>
(function() {
'use strict';
Polymer.FirebaseDatabaseBehaviorImpl = {
properties: {
db: {
type: Object,
computed: '__computeDb(app)'
},
ref: {
type: Object,
computed: '__computeRef(db, path)'
},
/**
* Path to a Firebase root or endpoint. N.B. `path` is case sensitive.
*/
path: {
type: String,
observer: '__pathChanged',
value: null
}
},
observers: [
'__onlineChanged(online)'
],
_setFirebaseValue: function(path, value) {
this._log('Setting Firebase value at', path, 'to', value)
var key = value && value.$key;
if (key) {
value.$key = null;
}
var result = this.db.ref(path).set(value);
if (key) {
value.$key = key;
}
return result;
},
__computeDb: function(app) {
return app ? app.database() : null;
},
__computeRef: function(db, path) {
if (this.ref) {
this.ref.off();
}
if (db == null ||
path == null ||
path.split('/').slice(1).indexOf('') >= 0) {
return null;
}
return db.ref(path);
},
__pathChanged: function(path, oldPath) {
if (oldPath != null) {
this.syncToMemory(function() {
this.data = this.zeroValue;
});
}
},
__onlineChanged: function(online) {
if (!this.ref) {
return;
}
if (online) {
this.db.goOnline();
} else {
this.db.goOffline();
}
}
};
Polymer.FirebaseDatabaseBehavior = [
Polymer.AppStorageBehavior,
Polymer.FirebaseCommonBehavior,
Polymer.FirebaseDatabaseBehaviorImpl
];
})();
</script>