Angularfire:如何将一个对象推入 $firebaseArray 中的另一个对象?
Angularfire: How to push an object into another object in a $firebaseArray?
我在 firebase 中有这个对象:
Groups
-id1
name: a
-id2
name: b
-id3
name: c
我这样使用"Groups":
var ref = new Firebase("https://app.firebaseio.com/Groups");
$scope.groups = $firebaseArray(ref)
$scope.groups.$add({
"name": "d"
}).then(function (ref)
{
console.log('Added group');
}, function (error)
{
console.error("Error:", error);
});
其中一些组包含项目。如何添加数组并向其中推送一些项目?如何处理数组已经存在的情况?
这不起作用:
var group = Groups.$getRecord(id3)
if(!group.hasOwnProperty('items')){
group['items'] = []
}
group['items'].push({item: "an item"})
一般来说,avoid nested arrays and flatten data 可能的话。
如果这些项目由多个用户异步更新,您将有冲突和奇怪的行为,因为arrays in distributed data 杀死小猫。
直接在要添加的项目上创建数组并改用推送 ID:
var ref = new Firebase("https://app.firebaseio.com/Groups/d/items");
$scope.items = $firebaseArray(ref)
$scope.items.$add({ $value: "an item" });
或者,如果我们要在 groups
上同步并且真的想每次都将每个更改同步到每个组,那么这会更简单更好:
new Firebase("https://app.firebaseio.com/Groups/d/items").push("an item");
我在 firebase 中有这个对象:
Groups
-id1
name: a
-id2
name: b
-id3
name: c
我这样使用"Groups":
var ref = new Firebase("https://app.firebaseio.com/Groups");
$scope.groups = $firebaseArray(ref)
$scope.groups.$add({
"name": "d"
}).then(function (ref)
{
console.log('Added group');
}, function (error)
{
console.error("Error:", error);
});
其中一些组包含项目。如何添加数组并向其中推送一些项目?如何处理数组已经存在的情况?
这不起作用:
var group = Groups.$getRecord(id3)
if(!group.hasOwnProperty('items')){
group['items'] = []
}
group['items'].push({item: "an item"})
一般来说,avoid nested arrays and flatten data 可能的话。
如果这些项目由多个用户异步更新,您将有冲突和奇怪的行为,因为arrays in distributed data 杀死小猫。
直接在要添加的项目上创建数组并改用推送 ID:
var ref = new Firebase("https://app.firebaseio.com/Groups/d/items");
$scope.items = $firebaseArray(ref)
$scope.items.$add({ $value: "an item" });
或者,如果我们要在 groups
上同步并且真的想每次都将每个更改同步到每个组,那么这会更简单更好:
new Firebase("https://app.firebaseio.com/Groups/d/items").push("an item");