Benedek Gagyi
Benedek Gagyi

Reputation: 18

Multiple arrays and nested arrays with AngularFire

Everything is nice when you have only one array.

var ref = new Firebase("...");
var sync = $firebase(ref);
var test =  sync.$asArray();

So I have an array in test on which I can operate with $add, $save and $remove.

But what if I want a second array? I've tried this:

var ref = new Firebase("...");
var sync = $firebase(ref);
var test =  sync.$asArray();
var test2 =  sync.$asArray();

But if I add something to test2, it goes to the same DB as the one test uses. Am I using it wrong? Do I need to create a new Firebase db for each array?

Is it possible to create a dynamic number of arrays? Is it possible, to nest arrays into arrays? Example: a list of todo lists.

Upvotes: 0

Views: 1178

Answers (2)

Benedek Gagyi
Benedek Gagyi

Reputation: 18

It wasn't an easy ride, but finally I found a simple way to do this. Check it out here.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 600120

In your example you call $asArray on the same $firebase sync twice, so naturally this leads to two references to the same array.

If you want two separate arrays, you'll need to set up a separate reference to the data for each of them:

var ref1 = new Firebase("https://yours.firebaseio.com/array1");
var ref2 = new Firebase("https://yours.firebaseio.com/array2");
var sync1 = $firebase(ref1);
var sync2 = $firebase(ref2);
var test =  sync1.$asArray();
var test2 =  sync2.$asArray();

Upvotes: 1

Related Questions