IndexedDB in Backbone

As the IndexedDB API is more complex than localStorage, it will be more difficult to create an IndexedDB driver for Backbone as we did with localStorage; in this section, you will use what you have learned about IndexedDB in order to build a driver for Backbone.

The driver should open a database and initialize the stores when it is created for the first time:

// indexedDB/dataStore.js 'use strict'; var Backbone = require('backbone'); const ID_LENGTH = 10; var contacts = [ // ... ]; class DataStore { constructor() { this.databaseName = 'contacts'; } openDatabase() { var defer = Backbone.$.Deferred(); // If a database connection is already active use it, // otherwise open a new connection if (this.db) { defer.resolve(this.db); ...

Get Mastering Backbone.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.