yathit / ydn-db

Javascript database module for Indexeddb, Web SQL and localStorage storage mechanisms supporting version migration, advanced query, SQL and transaction.
Apache License 2.0
503 stars 41 forks source link

how to implement promise-based CRUD events? #46

Closed hliyan closed 9 years ago

hliyan commented 9 years ago

from what i understand of the docs, the following adds a single event listener for all update events:

db.addEventListener(['updated'], function(event) {

how do i get a callback for the last update event, or a specific one? i'm trying to wrap YDN with something like this:

mystore.save('users', { name: 'joe' }, 24).then(function(result) {
   // handle something here
});
yathit commented 9 years ago

Dispatching events are expensive and won't emit unless requested during database initialisation.

See installable events section http://dev.yathit.com/ydn-db/doc/setup/events.html

Using events for application logic are not encouraged. There should be better alternative.

hliyan commented 9 years ago

just noticed the return type of add(). can i do it this way:

var req = db.add('users', theObject, theId);
req.done = function() {
};
req.fail = function() {
};
hliyan commented 9 years ago

looks like i should have looked at the doc closer: http://dev.yathit.com/api/ydn/db/request.html

your request already implements a then(success, fail)

sorry