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

标签 javascript firebase firebase-realtime-database polymer polymer-1.0

我正在尝试与 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/

相关文章:

android - 得到 child 的 child

ios - Firebase 扇出数据以更新特定字段删除其他兄弟字段

javascript - 音量的 react 输入范围 slider 不起作用

javascript - 如何在 vue.js 2 上获取响应 ajax?

angular - Angular6中添加内联类型时出错

javascript - 从 Firebase 获取一个值并将其更新为当前值 + 1

java - 将项目添加到回收站 View 的底部

javascript - 在嵌入中绑定(bind) src 在 chrome 中不起作用

javascript - 如何将 bootstrap 4 模态用作 "block ui"- 阻止输入并显示 "wait a minute.."标签的模态屏幕?

swift - 将文档引用传递给第二个 View Controller