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

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

问题描述

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

这是我有一个示例$ 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 (错误){
console.log(Error:,error);
});

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

  var article = new Firebase(FIREBASE_URI).child('Articles'); 
$ b $ article.push({
title:$ scope.article.title,
post:$ scope.article.post
},function(error){
if(error){
console.log(Error:,error);
}
});

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


<解决方案如何推/ $添加和设置/ $保存相关

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

何时使用push和何时使用set



虽然以上是一个很难的事实,但这一点更为主观。所以不要盲目复制它,但要弄清楚它是如何适用于你的情况的。



通常你应该使用 set ) / $ save()如果您的数据库中已经存在一个对象与具有自然键的对象。如果这些项目没有自然键,或者您希望按时间顺序对它们进行排序,那么通常使用 push() / $ add()为您生成密钥。这将确保项目被存储在一个唯一的键下,而更新的项目显示在列表中。



所以如果你和用户一起工作,你会通常使用诸如 ref.child('users')。child(user.uid).set(user) code>。另一方面,如果您将聊天消息添加到聊天室,则会使用类似于 ref.child('chat')的内容。push({name:'Adam Youngers','Firebase push()vs Angularfire $ 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()?

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);
        });

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?

解决方案

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'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.

When to use push and when to use set

While the above is a hard truth, this bit is more subjective. So don't blindly copy it, but figure out how it applies to your situation.

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.

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.

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()vs Angularfire $ save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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