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 APPSAdding the Ajax URL to the Main Manifest or FALLBACK SectionsThe worst thing you can do at this point is add the Ajax URL to the main section of the manifest. Thebrowser will treat the URL like any other resource, downloading and caching the content when themanifest is processed. When the client makes the Ajax request, the browser will return the content fromthe application cache, and the data won’t be updated until a manifest change triggers a cache update.The result of this is that your users will be working with stale data, which is generally contrary to thereasoning behind making the Ajax request in the first place.You get pretty much the same result if you add the URL to the FALLBACK section. Every request, evenwhen the browser is online, will be satisfied by whatever you set as the fallback, and no request will everbe made to the server.Adding the Ajax URL to the Manifest NETWORK SectionThe best approach (albeit far from ideal) is to add the Ajax URL to the NETWORK section of the manifest.When the browser is online, the Ajax requests will be passed to the server, and the latest data will bepresented to the user.The problems start when the browser is offline. There are two different approaches to handling Ajaxrequests in an offline browser. The first approach, which you can see in Google Chrome, is that the Ajaxrequest will fail. Your Ajax error handler will be invoked, and there is a clean failure.The other approach can be seen in Firefox. When the browser is offline, Ajax requests will beserviced using the main browser cache if possible. This creates the odd situation where the user will getstale data if a request <strong>for</strong> the same URL was made be<strong>for</strong>e the browser went offline and will get an error ifthis is the first time that the URL has been asked <strong>for</strong>.Understanding the POST Request BehaviorThe way that POST requests are handled is a lot more consistent than <strong>for</strong> GET requests. If the browser isonline, then the POST request will be made to the server. If the browser is offline, then the request willfail. This is true <strong>for</strong> POST requests that are made using regular HTML and <strong>for</strong> POST requests made usingAjax.This leads to annoyed users because POSTing a <strong>for</strong>m usually comes after some period of activity ontheir part. In the case of the CheeseLux example, the user will have paged through the categories andentered the amounts of each product they require. When they come to submit their order, the browserwill show an error page. You can’t even use the FALLBACK section of the manifest to nominate a page to beshown instead of the error.The only sensible thing to do is to intercept the <strong>for</strong>m submission and use the navigator.onLineproperty and events to monitor the browser status and prevent the user from trying to post contentwhen the browser is offline. In Chapter 6, I’ll show you some techniques <strong>for</strong> preserving the result of theuser’s ef<strong>for</strong>t, ready <strong>for</strong> when the browser comes back online.135www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!