Skip to content

Dexie.on.versionchange

David Fahlander edited this page Aug 18, 2016 · 12 revisions

Syntax

db.on("versionchange", function (event) {});

Description

The "versionchange" event occurs if another indexedDB database instance needs to upgrade or delete the database. If you do not subscribe to this event, Dexie has a built-in default implementation that will close the database immediately and fire a VersionChangeError error event to db.on('error'). This will resume the upgrading process of the other page.

To override the default behavior, your subscriber function must return false.

NOTE

After an upgrade has been made from another window, your current window will run on code that is written for the new model that has been upgraded to. A webapp should typically update itself when this event occur. For a singe-page application this normally means reloading the current location to refresh the HTML and JS code from server.

Sample

var db = new Dexie("MyDB");
db.on("versionchange", function(event) {
    if (confirm ("Another page tries to upgrade the database to version " +
                  event.newVersion + ". Accept?")) {
        window.location.reload(); // Refresh current webapp so that it starts working with newer DB schema.
    } else {
        return false; // Will let user finish its work in this window and block the other window from upgrading.
    }
};
Clone this wiki locally