Reputation: 2955
I have this object in firebase:
Groups
-id1
name: a
-id2
name: b
-id3
name: c
I use "Groups" this way:
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);
});
Some of these groups contain items. How do I add an array and push some items in it? How to handle the case where the array already exists?
This doesn't work:
var group = Groups.$getRecord(id3)
if(!group.hasOwnProperty('items')){
group['items'] = []
}
group['items'].push({item: "an item"})
Upvotes: 1
Views: 756
Reputation: 40582
In general, avoid nested arrays and flatten data where possible.
If these items are updated asynchronously by multiple users, you will have conflicts and strange behaviors, because arrays in distributed data kill baby kittens.
Create your array directly on the items to be added and use push ids instead:
var ref = new Firebase("https://app.firebaseio.com/Groups/d/items");
$scope.items = $firebaseArray(ref)
$scope.items.$add({ $value: "an item" });
Or, if we're going to synchronize on groups
and really want to sync every change to every group every time, then this would be simpler and better:
new Firebase("https://app.firebaseio.com/Groups/d/items").push("an item");
Upvotes: 2