聚合物:firebase数据库没有使用firebase元素进行更新(Polymer: firebase DB not updating using firebase

编程入门 行业动态 更新时间:2024-10-28 08:26:57
聚合物:firebase数据库没有使用firebase元素进行更新(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

无论如何,无论我在我的聚合物应用程序中尝试,FireBase都不会更新。 有关如何更新Firebase的建议?

I'm trying to setup a firebase connection with Polymer 1.0:

<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) { ... }

Here is how my firebase db looks like:

{ ... stats: { count: 0 } }

Now, inside the _handleFirebaseStats I receive { count: 0 }, that part works great. But I would also like to save data back to firebase. I've tried:

this.$.fstats.set({count: 2}); this.$.fstats.set({stats: {count: 2}}); this.stats.count = 2; // etc

Anyway, whatever I try in my polymer app, FireBase is never updated. Any suggestions on how to update firebase ?

最满意答案

根据我最初的假设(这是完全错误的) ,我认为这个行为与firebase-document元素有关,事实上,这个行为在Polymer的数据绑定系统中已经很好的定义了。

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

this.stats = {count: 2};

解决方案2:让系统知道路径绑定已更新。

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

解决方案3:让聚合物为您处理路径绑定的东西。

this.set('stats.count', 2);

直接从文档 :

这个系统“正常工作”,以至于由于绑定到发生更改的通知自定义元素属性而出现对对象子属性的更改。 但是,有时候命令式代码需要直接更改对象的子属性。 由于我们避免了更复杂的观察机制,例如Object.observe或dirty-checking,以便为大多数常见用例实现最佳的启动和运行时性能,因此直接更改对象的子属性需要用户进行合作。

具体而言,Polymer提供了两种允许将此类更改通知给系统的方法:notifyPath(path,value)和set(path,value),其中path是标识路径(相对于主机元素)的字符串。

另外还有一个Polycast ,Rob Dodson非常详细地解释了这个问题。

As per my initial assumption (which was completely wrong), I thought the behavior had something to do with firebase-document element, infact the behavior is well defined inside the Polymer's Data Binding system.

Solution 1: Replace the whole property.

this.stats = {count: 2};

Solution 2: Let the system know a Path Binding has been updated.

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

Solution 3: Let the Polymer handle the path binding stuff for you.

this.set('stats.count', 2);

Straight from the 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).

Also there is a Polycast where Rob Dodson explains this stuff in great detail.

更多推荐

本文发布于:2023-08-01 13:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1360386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:聚合物   元素   数据库   firebase   updating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!