聚合物:firebase集合总是返回空数组

Polymer: firebase-collection always returns empty array

本文关键字:返回 数组 firebase 集合 聚合物      更新时间:2023-09-26

我尝试使用firebase集合,但无论我尝试什么,我总是收到一个空数组(在firebase中我有几个条目)

<dom-module id="bar-foo">
    <template>
        <firebase-collection
           id="barFoo"
           location="https://bar-foo.firebaseio.com/items"
           data="{{items}}"></firebase-collection>
        <firebase-document location="https://bar-foo.firebaseio.com/item"
           data="{{item}}"></firebase-document>
    </template>
    <script>
        class BarFoo {
            beforeRegister() {
                this.is = 'bar-foo';
                this.properties = {
                    items: {
                        notify: true,
                        observer: 'updated',
                        type: Array
                    }
                }
             }
             updated(newList) {
                 // newList === this.items === []
                 ...
             }
        }
        Polymer(BarFoo);
   </script>
</dom-module>

firebase-document元素有效!为什么我收到一个空列表,有什么建议吗?

此外,当我手动将一个新项目添加到firebase集合时,我会在控制台中得到以下内容

this.$.barfoo.add('aaaa')
    firebase-query-behavior.html:182 Adding new item to collection with value: aaaa
    firebase-query-behavior.html:182 Firebase Event: "child_added" aaaa null
    U {k: Yh, path: L, n: ae, lc: false}

在firebase中什么也没发生:(

不要使用观察者。当我四处玩耍时,我发现观测者只会发射一次:在第一次发射时。我不能告诉你为什么。

幸运的是,当数据发生变化时,firebase集合会触发事件:https://github.com/GoogleWebComponents/firebase-element/issues/86

添加事件侦听器而不是属性观察器:

this.listeners = {
  'barFoo.firebase-value': 'updated'
}

PolymerLabs的此示例使用事件侦听器而不是观察器:https://github.com/PolymerLabs/todo-list/blob/master/app/elements/todo-data/todo-data.html

希望这能有所帮助,我将继续寻找一些关于firebase集合的观察者行为的解释。