问题描述
Angularfire 0.8中的 $ save()
让我感到困惑。 这是一个简单的例子 - 一个片段从我的controllers.js文件:
.controller('LandingPageController',['$ scope','$ firebase',function ($ scope,$ firebase){
$ scope.addNode = function(){
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/');
$ var fbr = $ firebase(FB);
fbr。$ set(1,{firstname:'James'});
}
$ scope.addAttribute = function(){
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/1');
var fbr = $ firebase(FB)。$ asObject();
fbr.lastname =Bond;
fbr。$ save();
}
}])
当调用 addNode()
的时候,果然在我的firebase中创建了一个节点:
addAttribute()被调用时,整个记录被替换,而不是我所期望的,这是'lastname'属性被添加。
$ b
我毫不怀疑误解了文档。任何人都可以帮忙吗?
更新:
好的,我需要等到对象被加载。现在,在将 addAttribute
更改为:
$ scope.addAttribute = function(){
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/1');
var fbr = $ firebase(FB)。$ asObject(); ();
fbr。$ loaded()。then(function(){
fbr.lastname =Bond;
fbr。$ save();
});
:
$ b
- a
FirebaseObject
(由$ asObject返回()
)没有$ update
方法。 - 当您调用
$ save()
在FirebaseObject
完全加载之前,最终可能会删除其他属性
要修补现有的数据,您可以:
- 或者等待整个对象(如你在更新问题时所做的那样)
- 或直接调用
$ firebase。$ update
$ firebase(FB)。$ update({lastname:Bond} );
最后一种方法的优点是不需要拉下整个对象,只需要更新单一财产。请注意,在大多数情况下,这可能是,但仍然...
The $save()
in Angularfire 0.8 is confusing me.
Here's a minimal example - a snippet from my controllers.js file:
.controller('LandingPageController', ['$scope','$firebase', function($scope,$firebase) {
$scope.addNode = function() {
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/');
var fbr = $firebase(FB);
fbr.$set(1,{firstname: 'James'});
}
$scope.addAttribute = function() {
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/1');
var fbr = $firebase(FB).$asObject();
fbr.lastname = "Bond";
fbr.$save();
}
}])
When addNode()
is called, sure enough, a node is created in my firebase:
But when addAttribute()
is called, the entire record is replaced, rather than what I expected, which was for the 'lastname' attribute to be added.
I've no doubt misunderstood the docs. Can anyone help?
Update:
OK, I needed to wait until the object was loaded. It works now, after changing addAttribute
to:
$scope.addAttribute = function() {
var FB = new Firebase('https://protodb.firebaseio.com/testrecords/1');
var fbr = $firebase(FB).$asObject();
fbr.$loaded().then(function() {
fbr.lastname = "Bond";
fbr.$save();
});
}
As you found out yourself already:
- a
FirebaseObject
(as returned by$asObject()
) does not have a$update
method. - when you call
$save()
on aFirebaseObject
before it is completely loaded, you may end up deleting other properties
To patch existing data you can:
- Either wait for the entire object to be loaded (as you did in your update to the question)
- Or call
$firebase.$update
directly
$firebase(FB).$update({ lastname: "Bond" });
This last approach has the advantage that you don't pull down the entire object, only to update a single property. Note that this is probably premature optimization in most cases, but still...
这篇关于如何使用AngularFire对对象进行部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!