AJAX - The Complete Reference

(avery) #1

544 Part III: Advanced Topics


our communication will fail so our library will invoke any onFail callback we have. We
modify our callback so that upon failure, we write the change to our local database and set a
global dirty flag variable (g_dirty) that we use to signal that things are different offline than
they are online. If we fail, we also change the visual status to let the user know they are offline.
When requests are going through as normal, we call our onSuccess callbacks but we
still update our local data store with the same changes made online. Upon every successful
request, we have to assume the previous request might not have been successful and check
the dirty flag. If it indicates we are out of sync, we call a special sync function to make sure
both the local and online application state match. We also update our online status as up
when a request goes through. Simple usage of the to-do application on- and offline is shown
in Figure 10-14.
The code is a bit involved to present it in paper, so we suggest you trace it carefully
online. Entrance to this Gears application can be found at http://ajaxref.com/ch10/
gearstodo.

FIGURE 10-14 To-do list, offl ine and on
Free download pdf