我正在尝试与 Polymer 1.0 建立 Firebase 连接:
<link rel="import" href="/bower/firebase-element/firebase-document.html">
...
<firebase-document id='fstats'
location="https://polymer-redux.firebaseio.com/stats"
log="true"
data="{{stats}}"></firebase-document>
...
this.properties = {
stats: {
type: Object,
observer: '_handleFirebaseStats',
notify: true
}
...
_handleFirebaseStats(stats) {
...
}
这是我的 firebase 数据库的样子:
{
...
stats: { count: 0 }
}
现在,在 _handleFirebaseStats
中,我收到了 { count: 0 }
,那部分效果很好。但我也想将数据保存回 firebase。我试过:
this.$.fstats.set({count: 2});
this.$.fstats.set({stats: {count: 2}});
this.stats.count = 2;
// etc
无论如何,无论我在 Polymer 应用程序中尝试什么,FireBase 都不会更新。关于如何更新 firebase 有什么建议吗?
最佳答案
根据我最初的假设(这是完全错误的),我认为该行为与 firebase-document
有关元素,事实上,行为在 Polymer's Data Binding 中得到了很好的定义。系统。
解决方案 1:替换整个属性。
this.stats = {count: 2};
方案二:让系统知道一个Path Binding已更新。
this.stats.count = 2;
this.notifyPath('stats.count', this.stats.count);
解决方案 3:让 Polymer 为您处理路径绑定(bind)。
this.set('stats.count', 2);
直接来自 docs :
This system “just works” to the extent that changes to object sub-properties occur as a result of being bound to a notifying custom element property that changed. However, sometimes imperative code needs to change an object’s sub- properties directly. As we avoid more sophisticated observation mechanisms such as Object.observe or dirty-checking in order to achieve the best startup and runtime performance cross-platform for the most common use cases, changing an object’s sub-properties directly requires cooperation from the user.
Specifically, Polymer provides two methods that allow such changes to be notified to the system: notifyPath(path, value) and set(path, value), where path is a string identifying the path (relative to the host element).
还有一个 Polycast Rob Dodson 在其中详细解释了这些东西。
关于javascript - Polymer:firebase DB 不使用 firebase-element 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053971/