Game Development Reference
In-Depth Information
dow.indexedDB object. Deleting a database is also done through the indexedDB
object, as we'll see soon.
In order to open a database (or create one if it doesn't exist yet), we simply call the
indexedDB.open method, passing in a database name, along with a version num-
ber. If no version number is supplied, the default version number of one will be used
as shown in the following code snippet:
var dbName = "myDatabase";
var dbVersion = 1;
var request = indexedDB.open(dbName, dbVersion);
As you'll soon notice, every method for asynchronous requests in IndexedDB (such
as indexedDB.open , for example), will return a request object of type IDBRequest,
or an implementation of it. Once we have that request object, we can set up callback
functions on its properties, which get executed as the various events related to them
are fired, as shown in the following code snippet:
var dbName = "myDatabase";
var dbVersion = 1;
var db = null;
var request = indexedDB.open(dbName, dbVersion);
request.onerror = function(event) {
console.log("Error:", event);
};
request.onsuccess = function(event) {
db = event.target.result;
};
IDBOpenDBRequest
As mentioned in the previous section, once we make an asynchronous request to
the IndexedDB API, the immediately returned object will be of type IDBRequest. In
the particular case of an open request, the object that is returned to us is of type
IDBOpenDBRequest. Two events that we might want to listen to on this object were
shown in the preceding code snippet ( onerror and onsuccess ). There is also a
very important event, wherein we can create an object store, which is the foundation
Search WWH ::




Custom Search