indexeddb.Source
is installed into your project's lib directory, much like
any other Enyo plugin, for example layout
or onyx
. See this post in Enyo Wiki for details.
Generally, you want to add indexeddb.Source
plugin as a git submodule of your
project, placed under lib/indexeddb
directory.
One way to do that is to run the following at your project's root directory:
git submodule add https://github.com/vtomilin/EnyoIndexedDBSource.git lib/indexeddb
Then you should add indexeddb.Source
dependency to your top-level package.js
file:
enyo.depends(
'$lib/layout',
'$lib/onyx',
'$lib/indexeddb',
// Other dependencies...
);
Create an instance of indexeddb.Source
sometime during your Applicaiton
startup and introduce it to enyo.store:
create: enyo.inherit(function(sup) {
return function() {
var idbSource;
sup.apply(this, arguments);
try {
idbSource = new indexeddb.Source({
dbName: 'users',
dbVersion: 1,
dbInitSchema: this.initSchema.bind(this)
});
enyo.store.addSources({
'idb': idbSource
});
} catch(error) {
this.error('Failed to initialize IDB:', error);
}
};
})
A global signal event, onIDBOpened
will be fired when database is initialized
and ready for use, or if its initialization failed, in which case the event
will have error
property set to a particular error. If database has been
successfully opened, then the event will contain a 'db' property, which holds
the IDBDatabase object.
onIDBReady: function(sender, event) {
if (event.error) {
this.error('Failed to initialize IndexedDB:', event.error);
return true;
}
this.$.users.fetch({
source: 'idb',
success: function(rec, opts, res) {
this.log('Great success, starting...');
this.start();
}.bind(this),
fail: function(rec, opts, error) {
this.error(error);
}.bind(this)
});
return true;
}
In the above example this.$.users
is enyo.Collection
object, configured in
the sample application:
components: [
{
kind: 'enyo.Signals', onIDBOpened: 'onIDBReady'
},
{
name: 'users',
kind: 'enyo.Collection',
model: 'db.IDBUser',
url: 'user'
}
]
Further, you simply use data management methods(fetch
, commit
, destroy
),
provided by Enyo's data framework(enyo.Collection
, enyo.Model
).
Whereas commit
and destroy
generally do not require additional options,
fetch
frequently does. These are options, which control its behavior:
- opts.params.index is an optional index name to use in the object store. If not specified, a default key(primary) will be used.
- opts.params.key is an optional key or key range. If the key is a simple value
then it is used directly to locate records, which match it. The key range
is defined by an object, which should define appropriate properties:
- lower key's lower bound value. If not defined, then the key range is unbound from the lower end.
- upper key's upper bound value. If not defined, then the key
range is unbound from the upper end.
NOTE at least one,
lower
orupper
property must be given. - lowerOpen optional boolean indicating whether records, which match lower key should be excluded from the result set.
- upperOpen optional boolean indicating whether records, which match upper key should be excluded from the result set.
- opts.params.singleItem optional boolean(defaults to false), specifying
whether a single record is requested. This could be useful for efficiency
reasons, when pulling out an object by its primary key. If this option
is given, other options are ignored except
key
andindex
. - opts.params.direction optional direction string of dataset traversal,
which will determine the order, which records will be returned at.
Possible values are:
- next this is the default
- prev the dataset will be traversed in reverse
- nextunique
- prevunique
- opts.params.offset optional offset in result set to return values from.
- opts.params.limit optional number of records to return.
NOTE that rec.url(which is your Collection
or Model
url
property)
will be used to infer underlying object store to operate on.
Also see samples
directory for examples.
© Vitaly Tomilin
Licensed under LGPL v3