Game Development Reference
In-Depth Information
row in the spreadsheet). This goes on until the tenth object, after which no more ob-
jects exist in the result set (again, to go along with the spreadsheet metaphor, after
we fetch the last row, the next row after that is null - it doesn't exist). As a result, the
data store will call the onsuccess callback, and pass in a null object. If we attempt
to read properties in this null reference, as though we were working with a real object
returned from the cursor, the browser will throw a null pointer exception.
Instead of trying to reconstruct an object from a cursor one property at a time, we
could abstract this functionality away in a generic form. Since objects being persis-
ted into the object store can't have any functions, we're not allowed to keep such
functionality inside the object itself. However, thanks to JavaScript's ability to build
an object from a reference to a constructor function, we can create a very generic
object builder function as follows:
var TodoItem = function(task) {
this.task = task;
this.completed = false;
this.toHTML = function() {
var el = document.createElement("li");
el.textContent = this.task;
if (this.completed) {
el.style.textDecoration = "line-through";
}
return el;
};
};
function inflatObject(class, object) {
// 1. Create an instance of whatever class we
reference
var obj = new class();
// 2. Copy every property from the object
returned by the cursor
// into the newly created object
for (var property in object) {
obj[property] = object[property];
}
// 3. Return the inflated object
return obj;
Search WWH ::




Custom Search