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 6 STORING DATA IN THE BROWSER• Note IndexedDB is only one of two competing standards <strong>for</strong> storing complex data in the browser. The other is<strong>Web</strong>SQL. As I write this, the W3C is supporting IndexedDB, but it is entirely possible that <strong>Web</strong>SQL will make acomeback or, at least, become a de facto standard. I have not included <strong>Web</strong>SQL in this chapter because support<strong>for</strong> it is limited at present, but this is an area of functionality that is far from settled, and you should review thesupport <strong>for</strong> both standards be<strong>for</strong>e adopting one of them <strong>for</strong> your projects.It is still early days <strong>for</strong> IndexedDB, and as I write this, the functionality is available only throughvendor-specified prefixes, signifying that the browser implementations are still experimental and maydeviate from the W3C specification. Currently, the browser that adheres most closely to the W3Cspecification is Mozilla Firefox, so this is the browser I have used to demonstrate IndexedDB.• Caution The examples in this chapter may not work with browsers other than Firefox. In fact, they may notwork even with versions of Firefox other than the one I used in this chapter (version 10). That said, you should stillbe able to get a solid understanding of how IndexedDB works, even if the specification or implementations change.The IndexedDB feature is organized around databases that, like local and session storage, areisolated on a per-origin basis so that they can be shared between applications from the same origin.IndexedDB doesn’t follow the SQL-based table structure that is common in relational databases. AnIndexedDB database is made up of object stores, which can contain <strong>JavaScript</strong> objects. You can add<strong>JavaScript</strong> objects to object stores, and you can query those stores in different ways, some of which Idemonstrate shortly.The result of this approach is a storage mechanism that is more in keeping with the style of the<strong>JavaScript</strong> language but that ends up being slightly awkward to use. Almost all operations in IndexedDBare per<strong>for</strong>med as asynchronous requests to which functions can be attached so that they are executedwhen the operation completes. To demonstrate how IndexedDB works, I am going to create a CheeseFinder application. I will put the cheese product data into an IndexedDB database and provide the userwith some different ways of searching the data <strong>for</strong> cheeses they might like. Figure 6-2 shows the finishedweb app to help provide some context <strong>for</strong> the code that follows.157www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!