Xnn04
Xnn04

Reputation: 91

Google realtime api - accessing arrays inside a collaborative list

So I want to have a collaborative list object in which I store other lists, such as:

var tasks = model.createList();
tasks.pushAll([['Do', 'Buy', 'go'], ['1', '23', '44', '21']]);

My question is: how can I access those elements? Normally, something like tasks[i][j] would work but in the case of the realtime api it does not. Of course I can use the .asArray() method, however it is of no use to me when I want to add something to the specific array inside that list object.

EDIT: Ok, so you can get them with for example: Tasks.get(i)[j]But how can new elements be added inside of a specific array? tasks.get(i).push('something') won't work.

Upvotes: 0

Views: 59

Answers (2)

Christopher Best
Christopher Best

Reputation: 476

var dumbList = tasks.get(i).slice();

will get you a copy of the normal (non-collaborative) array you've put there. You can change it and then set the value back in the collaborative array to put the new version in your document.

dumbList.push('new item');
tasks.set(i, dumbList);

that will trigger a ValuesChangedEvent on tasks.

Upvotes: 1

adjuremods
adjuremods

Reputation: 2998

Since model.createList() returns a CollaborativeList class, the getmethod should do the trick (as you indicated). push should still be able to work as it will add an item at the end of the list. Another method you can use other than push would be the insert, but you'll have to specify at what index it will be added.

Also, please check out the Data Modeling Guidelines as it contains ways to design efficient and correct data models for using Realtime API.

Upvotes: 0

Related Questions