Sending Your Offline Data to jsonengine

Sending Your Offline Data to jsonengine

===

Sending Your Offline Data to jsonengine

As jsonengine expects input in JSON format, we have to convert our offline SQL data to JSON format. The global flow is quite simple:

1. Select all the offline records.

2. Parse them to JSON format.

3. Send them to jsonengine. Selecting all the database records is something we have already done before, so we can

copy and paste an existing function, clean it up, add some JSON conversion goodness, and finally send the data to our online storage service, as follows:

function synchronizeOnline(callback) { db.transaction(function(transaction) { transaction.executeSql( "SELECT rowid as id, * FROM task ", [], function (transaction, results) {

var tasksSynchronized = 0;

// initialise an array to hold the tasks // read each of the rows from the db, and create tasks for (var ii = 0; ii < results.rows.length; ii++) {

var task = new module.Task(results.rows.item(ii)),

taskJson = JSON.stringify(task);

$.post("/_je/tasks", {_doc:taskJson,_docId:task.id}, function() {

// once the post has completed, increment the counter tasksSynchronized += 1;

// and check to see if we have finished the sync operation if (callback && (tasksSynchronized === results.rows.length)) {

callback(tasksSynchronized, true); } // if

}); } // for

// fire the callback and provide information on the number // of tasks that were updated if (callback) {

callback(results.rows.length, false); } // if } ); }); }

Let’s focus on the for loop, where the interesting stuff happens. Firstly, we use the JSON library to serialize a Task object that we have retrieved from the database to a JSON string. Next, we use the jQuery post function to send that serialized data (using a POST request) to jsonengine for storage.

CHAPTER 5: Synchronizing with the Cloud

You can see that our post call takes three arguments: The first argument is the url that we are making the POST request to.

In this case, our storage URL is the same as our application; we just prefix it with _je (for “JSON storage”)—this is simply a jsonengine convention.

Our next argument contains the data that we are sending through the jsonengine. For jsonengine to be able to save our data effectively, it needs two things:

A document ( _doc)—this is the data that is going to be stored.

A document id ( _docId)—the unique key of our document that we are storing. As jsonengine is smart enough to detect whether the provided _docId already exists, and will either create a new record or update the existing record.

Finally, we supply a success callback for the post function and this is called once the request has been successfully completed.

What is happening here? First, we’re using jQuery’s post function. This is one of jQuery’s most powerful functions when we have to deal with network communication. This function will perform a HTTP POST request, and of course we pass an array of parameters: ("/_je/tasks", {_doc:taskJson,_docId:task.id}, success callback).

The first parameter is the URL to which we are posting our request. In this case, our storage URL will be the same as our application; we just prefix it with _js (for “JSON storage”)—this is simply a jsonengine convention.

The second parameter, {_doc:taskJson,_docId:task.id}, is more jsonengine specific. It’s a nested parameter’s map. _doc defines the name of our storage record, and it can

be compared to a database table name. The value we provide is the freshly JSON- converted string; in jsonengine terms, it’s called the JSON document. Then, _docId represents the unique key of our record. As you can see, we are using the index of our for loop because it’s the same index as the ID of each of the offline task’s entries.

NOTE: Where does jsonengine store the submitted JSON strings? It’s using Google App Engine’s data store. As Google’s online storage solution, Google App Engine takes care of all the distribution, replication, and load balancing of data. In fact, jsonengine is just an abstraction layer above the data store to perform the storage of JSON documents transparently.

CHAPTER 5: Synchronizing with the Cloud