Skip to content

Building Addons

David Fahlander edited this page May 6, 2014 · 22 revisions

Dexie is designed for being extended by addons.

Methods for Extending Dexie

This array contains functions that extends Dexie. An addon may register itself in Dexie.addons by using Dexie.addons.push(fn). Example:

(function(){

    function ForEachAddon (db) {
        // Makes it possible to use forEach() instead of each() on collections.
        db.Collection.prototype.forEach = db.Collection.prototype.each;
    }

    Dexie.addons.push(ForEachAddon);
})();

Understanding where Dexie classes reside

Dexie classes such as Collection, Table, WriteableTable are created in the closure scope of the Dexie constructor. To extend the prototypes you must work with an instance of Dexie. See example above.

Overriding existing methods

To override an existing method on any class, you can just change its current value to your own function. A handy function for overriding is the Dexie.override() function. Example:

db.Collection.each = Dexie.override (db.Collection.each, function (originalEach) {
    return function () {
        var returnValue = originalEach.apply(this, arguments);
        // Do some more stuff
        return returnValue;
    }
});

Protected methods

The API Reference does not document the protected methods and properties of Dexie. You may however look into the code and find lots of methods and properties with a leading underscore. Your addon may then override protected methods to change the behaviour of the database engine. To see an example of this, look at how Dexie.Observable.js is implemented.

Clone this wiki locally