Changing a Realtime Collaborative List
Thank you for the answers. To summarize:
It appears that indexOf is not useful for finding an object in a Realtime
Collaborative List of objects. I needed to change one object in the list
and thought indexOf would be a quick method. So it looks like I have to
convert the Collaborative list to a javascript array, go through the array
to find my object, get the index, and then change the
object in the Collaborative list.
window.gapi.load('drive-realtime', startdemo);
function startdemo() {
var i;
var index;
/* Create an in memory document for testing. */
var doc = gapi.drive.realtime.newInMemoryDocument();
var model = doc.getModel();
var mylist = model.createList();
mylist.clear();
var apple = {"type": "apple","identifier": "100"};
mylist.push(apple);
var orange = {"type": "orange","identifier": "101"};
mylist.push(orange);
var banana = {"type": "banana","identifier": "102"};
mylist.push(banana);
var grape = {"type": "grape","identifier": "103"};
mylist.push(grape);
/* Convert the collaborative list to a javascript array. */
var myarray = mylist.asArray();
/* Show what is in the array */
for ( i = 0; i < myarray.length; i++ )
{
console.log("Original List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
}
/* Find the index for our object. */
for ( i = 0; i < myarray.length; i++ )
{
if ( myarray[i].identifier === "103" )
{
index = i;
i = myarray.length;
}
}
console.log("index = " +index);
/* We will replace grape at identifier 103 with apricot. */
var apricot = {"type": "apricot","identifier": "103"};
mylist.set(index,apricot);
/* Get a new array to show the modification. */
myarray = mylist.asArray();
/* Now show what is in the list */
for ( i = 0; i < myarray.length; i++ )
{
console.log("Modified List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
}
}
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
Changing a Google Realtime Collaborative List
Thank you for the answers. To summarize:
It appears that indexOf is not useful for finding an object in a Realtime
Collaborative List of objects. I needed to change one object in the list
and thought indexOf would be a quick method. So it looks like I have to
convert the Collaborative list to a javascript array, go through the array
to find my object, get the index, and then change the
object in the Collaborative list.
window.gapi.load('drive-realtime', startdemo);
function startdemo() {
var i;
var index;
/* Create an in memory document for testing. */
var doc = gapi.drive.realtime.newInMemoryDocument();
var model = doc.getModel();
var mylist = model.createList();
mylist.clear();
var apple = {"type": "apple","identifier": "100"};
mylist.push(apple);
var orange = {"type": "orange","identifier": "101"};
mylist.push(orange);
var banana = {"type": "banana","identifier": "102"};
mylist.push(banana);
var grape = {"type": "grape","identifier": "103"};
mylist.push(grape);
/* Convert the collaborative list to a javascript array. */
var myarray = mylist.asArray();
/* Show what is in the array */
for ( i = 0; i < myarray.length; i++ )
{
console.log("Original List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
}
/* Find the index for our object. */
for ( i = 0; i < myarray.length; i++ )
{
if ( myarray[i].identifier === "103" )
{
index = i;
i = myarray.length;
}
}
console.log("index = " +index);
/* We will replace grape at identifier 103 with apricot. */
var apricot = {"type": "apricot","identifier": "103"};
mylist.set(index,apricot);
/* Get a new array to show the modification. */
myarray = mylist.asArray();
/* Now show what is in the list */
for ( i = 0; i < myarray.length; i++ )
{
console.log("Modified List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
}
}
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>