angularjs - ValueChanges 和 SnapshotChanges,不再使用 Firebase AngularFire2 获取完整列表

标签 angularjs firebase rxjs angularfire2 angular2-observables

最近 AngularFire 如何处理对象/列表以及在整个应用程序中引用对象的方式发生了变化,我们遇到了一些严重的问题。

首要的是如何旧的AngularFireObject & AngularFireList与新的相比工作。我们的应用程序曾经/高度依赖于 $key值,因为我们广泛地去规范化(如推荐的那样)。

现在文档使用 map 示例来获取 $key值,但效果不一样,甚至 valueChanges()似乎不一样。

我不完全确定我们现在应该如何处理这些更改。

考虑:

老路

/* /items/
    a/{name: 'Dennis', city: 'Dallas'}
    b/{name: 'Katie', city: 'Austin'}
    c/{name: 'Will', city: 'Chicago'}
*/
let myAfList = this.af.list('path/to/items');
let itemsFirst, itemsSecond, itemsThird;

myAfList.subscribe(items => itemsFirst = items);

setTimeout(_ => myAfList.subscribe(items => itemsSecond = items), 1000);
setTimeout(_ => myAfList.subscribe(items => itemsThird = items), 2000);

/* Results for all three item arrays
    itemsFirst: [
        {name: 'Dennis', city: 'Dallas', $key: 'a'},
        {name: 'Katie', city: 'Austin', $key: 'b'}
        {name: 'Will', city: 'Chicago', $key: 'c'}
    ];
*/

所有三个订阅都正确获取了完整的项目数组,并设置为监听 future 的变化

值(value)变化的新方式:

let myAfList = this.af.list('path/to/items').valueChanges();
let itemsFirst, itemsSecond, itemsThird;

myAfList.subscribe(items => itemsFirst = items);

setTimeout(_ => myAfList.subscribe(items => itemsSecond = items), 1000);
setTimeout(_ => myAfList.subscribe(items => itemsThird = items), 2000);

/* Results for ONLY itemsFirst
    itemsFirst: [
        {name: 'Dennis', city: 'Dallas'},
        {name: 'Katie', city: 'Austin'}
        {name: 'Will', city: 'Chicago'}
    ];
*/
ItemsFirst因为第一个订阅正确地获得了项目。其他两项一无所获,但所有三项都订阅了 future 的更改。没有一个有键值。

带有 $key 的 map

let myAfList = this.af.list('path/to/items').snapshotChanges()
            .map(actions => {
                return actions.map(action => {
                    const $key = action.payload.key;
                    const data = { $key, ...action.payload.val() };
                    return data;
                });
            });
let itemsFirst, itemsSecond, itemsThird;

myAfList.subscribe(items => itemsFirst = items);

setTimeout(_ => myAfList.subscribe(items => itemsSecond = items), 1000);
setTimeout(_ => myAfList.subscribe(items => itemsThird = items), 2000);

/* Results for ONLY itemsFirst
    itemsFirst: [
        {name: 'Dennis', city: 'Dallas', $key: a},
        {name: 'Katie', city: 'Austin', $key: b}
        {name: 'Will', city: 'Chicago', $key: c}
    ];
*/
$key现在在列表中,但它再次仅在第一个项目列表中......

所以快速观察是现在的对象是围绕核心 firebase 引用的非常简单的包装器,并且 valueChanges() 需要移动到子端,如:

let myAfList = this.af.list('path/to/items');
myAfList.valueChanges().subscribe(items => itemsFirst = items);

这行得通!耶!但是 key 呢?好吧,我必须一遍又一遍地写出 map 功能。或者创建我自己的函数,但它不在对象原型(prototype)上,所以我必须将它添加到我使用它的每个文件中......

我错过了什么?获取 $key 的正确方法是什么,订阅是否总是获得完整的值?

有一个更好的方法吗?

最佳答案

您可能想探索这种可能性:

// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push();

// Get the unique key generated by push()
var postId = newPostRef.key;

source

关于angularjs - ValueChanges 和 SnapshotChanges,不再使用 Firebase AngularFire2 获取完整列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115121/

相关文章:

javascript - 如何使用 ng-repeat 访问驻留在函数和循环中的 json 数组?

php - 使用 AngularJS 和 PHP 将图像上传到服务器路径

javascript - 文本输入焦点在 Cordova Android 应用程序上无法按预期工作(但在浏览器中的 cordova 服务上则正常)

angular - RxJs Observable 未调用完整方法

javascript - AngularJS 中的工厂 "is not defined"

android - Firebase 身份验证限制

android - taskSnapshot.getDownloadUrl() 已弃用

ios - Firebase - 使用未声明的标识符 'FIRAnalyticsConfiguration'

ios - 是否可以使用 RxSwift 在输入字段数组上执行自定义 Binder ?

angular - Linq First相当于TypeScript+rxjs