12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5 CREATING OFFLINE WEB APPSwindow.applicationCache.update();} else {window.applicationCache.swapCache();window.location.reload(false);}});});The window browser object supports the online and offline events that are triggered when thebrowser status changes. You can get the current status through the window.navigator.onLine property,which returns true if the browser is online and false if it is offline. Note that the L in onLine isuppercase. I have added an online observable data item to the view model, which I update in responseto the online and offline events. This is the same technique that I used <strong>for</strong> the application cache status,and it allows me to use the view model to propagate changes through to my markup. Listing 5-9 showsthe changes to the HTML elements that display the News and application cache control buttons.Listing 5-9. Adding Elements and Bindings to Respond to the Browser Online StatusGourmet European CheeseCheck <strong>for</strong> UpdatesApply UpdateNews(Offline)When the browser is online, the cache control and the News buttons are displayed. When thebrowser is offline, I replace the buttons with a simple placeholder. You can see the effect in Figure 5-6.• Tip You need to ensure that you have the right version of the offline content be<strong>for</strong>e taking the browser offline.Be<strong>for</strong>e running this example, you should either change the manifest or clear the browser’s history.128www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!