HTML and CSS Reference
In-Depth Information
Next, at the start of the init method in tasksController , add the following code:
storageEngine.init(function() {
storageEngine.initObjectStore('task', function() {
}, errorLogger)
}, errorLogger);
This code calls the init method on the storage engine, and when that succeeds, the success
callback is invoked, which in turn calls the initObjectStore method. This fulfils an import-
ant need: since the init success callback may not be called immediately in other implement-
ations of this API (i.e. those that perform their processing asynchronously), it is necessary
to ensure that init succeeds before calling the initObjectStore method. If we did not do
this, we may call initObjectStore while init was still processing.
This highlights the challenge of working with interfaces that can perform asynchronous
processing. It is often necessary to chain together a whole set of function calls, each inside
the callback of its predecessor. This makes code less elegant than the “chaining” approach
we saw with jQuery.
We will next implement the first interesting method of the storage engine: the save method.
Add the following as a new public method to the storageEngine module:
save: function(type, obj, successCallback, errorCallback) {
if (!initialized) {
errorCallback('storage_api_not_initialized', 'The storage engine has not been initialized');
} else if (!initializedObjectStores[type]) {
errorCallback('store_not_initialized', 'The object store '+type+' has not been initialized');
if (! { = $.now();
var savedTypeString = localStorage.getItem(type);
var storageItem = JSON.parse(savedTypeString);
storageItem[] = obj;
localStorage.setItem(type, JSON.stringify(storageItem));
Search WWH ::

Custom Search