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 APPSIn this listing, I have used the jQuery getJSON method. This is a convenience method that makes anAjax GET request <strong>for</strong> the JSON file specified by the first method argument, which is products.json in thiscase. When the Ajax requests has completed, jQuery parses the JSON data to create a <strong>JavaScript</strong> object,which is passed to the function specified by the second method argument. In my listing, the functionsimply takes the <strong>JavaScript</strong> object and assigns it to the products property of the view model. Theproducts.json file contains a superset of the data I have been defining inline. The same categories,products, and prices are defined, along with an additional description of each cheese. Listing 5-11 showsan extract from products.json.Listing 5-11. An Extract from the products.json File...{"id": "stilton", "name": "Stilton", "price": 9,"description": "A semi-soft blue cow's milk cheese produced in the Nottinghamshire region. Astrong cheese with a distinctive smell and taste and crumbly texture."},...In the listing I chain the getJSON method with a call to success. The success method is part of thejQuery support <strong>for</strong> <strong>JavaScript</strong> <strong>Pro</strong>mises, which make it easy to use and manage asynchronous operationslike Ajax requests. The function passed to the success method won’t be executed until the getJSONmethod has completed, ensuring that my view model is complete be<strong>for</strong>e the rest of my script is run.This approach to getting core data from JSON is a common one, especially where the data is sourcedfrom a different set of systems to the rest of the web app. And, if used carefully, it can ensure that theuser has the most recent data but still has the benefit of a cached application.Understanding the Default Ajax GET BehaviorThe browser treats an Ajax GET request in a very simple way. The request will fail if the Ajax request is <strong>for</strong>a resource that is not in the manifest, even when the browser is online.For my example application, this means that data is returned from the request and it dies a horribledeath. The function I passed as an argument to the getJSON method is executed only if the Ajax requestsucceeds, and the same is true <strong>for</strong> the function passed to the success method. Because neither functionis executed, the main part of my script code isn’t per<strong>for</strong>med, and I leave the user stranded. Worse, sincethe application cache control buttons are never set up, I don’t give the user a means to update theapplication to fix the problem.I have shown this scenario because it is very commonly encountered when programmers first startusing the application cache. I’ll show you how to make the Ajax connection work shortly, but first, thereare a couple of important changes to be made.Restructuring the ApplicationThe first change is to structure the application so that the core behavior that will get the user back out oftrouble will always be executed. My initial listing is just too optimistic, and I need to separate those partsof the code that should always be run. There are lots of different techniques <strong>for</strong> doing this, but I find thesimplest is just to create another function that is contingent on the jQuery ready event. Listing 5-12shows the changes I require to the script element.131www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!