Polymer:firebase DB 不使用 firebase-element 更新

Polymer: firebase DB not updating using firebase-element

我正在尝试设置与 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 element, infact the behavior is well defined inside the Polymer's Data Binding 系统有关。

解决方案 1:替换整个 属性。

this.stats = {count: 2};

解决方案 2:让系统知道 Path Binding 已更新。

this.stats.count = 2;
this.notifyPath('stats.count', this.stats.count);

解决方案 3:让 Polymer 为您处理路径绑定。

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 非常详细地解释了这些内容。