HTML and CSS Reference
In-Depth Information
Adding data to an IndexedDB database requires the creation of a transaction to define an
array of object stores you'll be using to store the data and the type of transaction needed,
in this case 'readwrite' . Once you have the transaction created, you then call its meth-
od objectStore , with the name of the object store you want to add a data item to. The
method will respond to this call by returning the object store. From here, adding the data
item to the store is easy. Call the object store's method add , and pass the new data item
to its only argument. The method will immediately return a request object. If you'd like
the application to respond to the object store's successful addition of the data item, then
define an event handler for the transaction's oncomplete event.
Now, let's see how this addition procedure can be applied to the application.
Step 6: Add new tasks from the Add Task view to the database
This code creates a new function called insertTask that manages the process of insert-
ing the task into the database and updating the display of the Task List view. InsertTask
first constructs a new task object from the Add Task form; second, it adds the task to the
IndexedDB database (or Web SQL if the browser doesn't support IndexedDB). Finally, it
triggers the callback function, updateView , when the task has been successfully added
to the database. Add the code from the following listing after the code from the previous
Listing 5.14. app.js—Adding new tasks
Search WWH ::

Custom Search