HTML and CSS Reference
In-Depth Information
}
The following lines in the save method:
var savedTypeString = localStorage.getItem(type);
var storageItem = JSON.parse(savedTypeString);
storageItem[obj.id] = obj;
localStorage.setItem(type, JSON.stringify(storageItem));
successCallback(obj);
can now be changed to:
var storageItem = getStorageObject(type);
storageItem[obj.id] = obj;
localStorage.setItem(type, JSON.stringify(storageItem));
successCallback(obj);
The
findAll
method will allow us to repopulate the saved tasks from
localStorage
back
into the
table
when the page is refreshed. The following is the implementation of
findAll
:
findAll : function(type, 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');
}
var result = [];
var storageItem = getStorageObject(type);
$.each(storageItem, function(i, v) {
result.push(v);
});
successCallback(result);
}
Again, we will step through the implementation line by line.
The first section of the method performs the same checks as the
save
method in order to
check
init
and
initObjectStore
have both been invoked.
Next, we initialize an array to return the objects available for the specified type. This en-
sures even if there are no saved objects an empty array will be returned: