Reputation: 25
My plan is to create a data model that is a collaborative map, containing collaborative lists, which contain custom object. However, I'm struggling to understand the google api documentation. With the goal of trying to create an event management tool, similar to the one demonstrated here http://commondatastorage.googleapis.com/io-2013/presentations/708%20-%20Realtime%20API%20IO%202013.pdf
function initializeModel(model) {
var map = model.createMap();
model.getRoot().set('map', map);
}
function onFileLoaded(doc) {
var map = doc.getModel().getRoot().get('map');
map(gapi.drive.realtime.EventType.VALUE_CHANGED, "some function which will update the project");
I think this creates the map, however, I have no idea how to access it to add a custom list the different branches of the map, and then custom objects to these in turn. I am very new to Google Realtime API. Any help is very much appreciated. Thanks,
Upvotes: 0
Views: 422
Reputation: 476
To create a collaborative map, use Model.createMap()
. You are doing so correctly in your initializeModel function:
var map = model.createMap();
To add an item to a collaborative map, use CollaborativeMap.set
. You are also doing in your initializeMap function when you add the map you create to the root map:
model.getRoot().set('map', map);
Note that all models start with a collaborative map called the root, which you can access with Model.getRoot()
. Therefore, after your initializeModel function, your document model looks like
{map: {}}
So you have successfully created a collaborative map that you could add collaborative lists to. In your onFileLoaded function you obtain a reference to that map with this line:
var map = doc.getModel().getRoot().get('map');
To add a list to the map, you would create a list:
var list = doc.getModel().createList();
Then add the list to the map:
map.set('mylist', list);
Now your document model looks like:
{map: {mylist: []}}
I'm not sure what the intent of your last line is. var map
is an object and is not callable. Usually you reference event types when you add event handlers. For example, if you wanted a function to run when a value is changed on the map, you could do this:
map.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED,
function(e) {
console.log('key ' + e.property + ' changed from value ' + e.oldValue + ' to value ' + e.newValue);
});
Upvotes: 2