160 likes | 202 Views
Offline. RIA. Internet Connection. connection is not guaranteed no connection means web applications will not work what can be done? can users still add data? retrieve data? make changes? what does developer have to do? (plan ahead). Connection Problem Solutions. appCache
E N D
Offline RIA
Internet Connection • connection is not guaranteed • no connection means webapplications will not work • what can be done? • can users • still add data? • retrieve data? • make changes? • what does developer have to do? (plan ahead)
Connection Problem Solutions • appCache • localStorage • indexedDB html js css jpg data
AppCache • developer can determine which files can be cached and which cannot • must carefully plan the policy for caching • manifest file is the controller cache index.html, style.css, funct.js pict1.jpg, pict2.jpg pict3.jpg,...
Sequence • user goes to web site • are files in cache? • yes: has the manifest changed? • yes: retrieve them from server and update cache • no: retrieve files from cache • no: retrieve them from server and store in cache
Manifest File • identify in attribute of html tag<html manifest="anyName.mf"> • 3 sections • CACHE: #files to be cached • NETWORK: #files not to be cached • FALLBACK: #substitute files
Manifest Example CACHE MANIFEST # 2015-05-28:v1 # Explicitly cached 'master entries'. CACHE: index.html style.css # Resources that require the user to be online. NETWORK: * # offline.html will be served in place of all other .html files FALLBACK: / /offline.html
Javascript API for appCache var appCache = window.applicationCache; appCache.update(); // Attempt to update the user's cache. if (appCache.status == window.applicationCache.UPDATEREADY) { appCache.swapCache(); // swap in the new cache.
Web Storage • data is stored only on the client side • about 5M is available • use key-value pairs • data is separated by origin (url) • two storage objects • localStorage → for long term storage • sessionStorage → for just the current session
Using localStorage Object • built into javascript • set data with localStorage.setItem("key","data") • if key does not exist, it creates it • if key does exist, it resets the value • retrieve with localStorage.getItem("key")
localStorage - other • use deleteItem to remove an key-value from storage • use clear() to remove all data • length returns number of data values
Web Database • indexedDB object works more like a database than localStorage • used to be relational but now it is like a NoSQL database • key-value based • value can be anything (number, string, object, array, blob, etc.
Database Structure • each origin can have one or more databases • each database has one or more data stores • each store has key-value records • key can be number, string, date or array • value can be anything (number, string, object, array, blob, etc. • transactions for multiple operations • cursors are used to iterator or multiple records
Operations • Asynchronous • each operation returns a request object • success handler for normal operation • fail or upgrade handlers for unusual situations
Transactions • create transaction • var trans = db.transaction(["students"],"readwrite"); • select object store • var objStore = trans.objectStore("students"); • start operation • var request = objStore.add(data); • handle events • request.onsuccess = function(event){ …
indexedDB Summary • much more complicated than localStorage • designed for a lot of data and fast retrieval • for more details see https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB