Firebase push() 与 Angularfire $save() [英] Firebase push() vs Angularfire $save()

查看:25
本文介绍了Firebase push() 与 Angularfire $save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angularfire .$save() 与 firebase .push() 相比如何?我知道 push() 会在存储数据时生成一个唯一的键,但我无法使用 angularfire 重新创建该行为.有没有办法或者我应该使用 .push(),如果是这样,你会在什么情况下使用 $save()?

How does angularfire .$save() compare to firebase .push()? I know push() will generate a unique key when data is stored, but I can't recreate the behavior using angularfire. Is there a way or should I be using .push() and if so, in what case would you use $save()?

这是我使用 $save() 的一个示例...

Here is one sample I have using $save()...

        var fb = new Firebase(FIREBASE_URI).child('Test');
        var article = $firebaseObject(fb);

        article.Foo = "bar";
        article.$save().then(function(fb) {
            console.log(fb.key() === article.$id); // true
        }, function(error) {
            console.log("Error:", error);
        });

另一个使用 .push()...

And another using .push()...

        var article = new Firebase(FIREBASE_URI).child('Articles');

        article.push({
            title:   $scope.article.title,
            post:    $scope.article.post
        }, function(error) {
            if (error) {
                console.log("Error:", error);
            }
        });

两者的优缺点和用例是什么?

What are the Pros/Cons and use cases for both?

推荐答案

push/$add 和 set/$save 的关系

AngularFire 的 $save() 方法是使用 Firebase 的 set() 方法实现的.两者都将数据保存到现有/已知位置.

How push/$add and set/$save relate

AngularFire's $save() method is implemented using Firebase's set() method. Both save data to an existing/known location.

Firebase 的 push() 操作对应于 AngularFire 的 $add() 方法.两者都会生成一个新的唯一子键,这意味着您可以使用它们在保证不与任何现有数据冲突的位置添加新数据.

Firebase's push() operation corresponds to AngularFire's $add() method. Both will generate a new unique child key, which means you can use them to add new data at a location that is guaranteed to not conflict with any existing data.

虽然以上是硬道理,但这一点更主观.所以不要盲目复制它,而是弄清楚它如何适用于你的情况.

通常情况下,如果您有一个已存在于数据库中的对象,您应该使用 set()/$save()如果您正在处理具有自然键的对象.

Typically you should be using set()/$save() if you either have an object that already exists in the database or if you are working with objects that have a natural key.

如果项目没有自然键或者您希望它们按时间顺序"排序,您通常会使用 push()/$add() 为您生成密钥.这将确保项目存储在唯一键下,并且较新的项目稍后显示在列表中.

If the items don't have a natural key or you want them to be sorted "chronologically", you'll typically use push()/$add() to generate a key for you. This will ensure that the items are stored under a unique key and that newer items show up later in the list.

因此,如果您与用户一起工作,您通常会使用诸如 ref.child('users').child(user.uid) 之类的东西将其存储在他们的 uid 下.设置(用户).另一方面,如果您要向聊天室添加聊天消息,您将使用类似 ref.child('chat').push({ name: 'Adam Youngers', 'Firebase push()vs Angularfire $save()' }).

So if you're working with users, you'll typically store then under their uid using something like ref.child('users').child(user.uid).set(user). On the other hand, if you're adding chat message to a chat room, you'll use something like ref.child('chat').push({ name: 'Adam Youngers', 'Firebase push() vs Angularfire $save()' }).

这篇关于Firebase push() 与 Angularfire $save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆