1

Im trying to use multiple deferred with jquery $.when but so far no luck, this is my code:

var req = $.when(db.count('items'),db.values('items'),db.get('config', 1));

req.done(function(count,r,config) {
  var currency = config.currency;
  if(count > 0){
    var n = r.length;
    for (var i = 0; i < n; i++) {                   
      var id    = r[i].id;
      var itemId = r[i].itemId;
      console.log('ID: '+id+' itemID: '+itemId+' Currency: '+currency);
    }
  }
});

My sample isn't working so hope you guys can help me, I searched everywhere for a solution. Thanks

Crash Override
  • 411
  • 6
  • 17

1 Answers1

0

I see. I will see how I could implement jquery deferred list. Although ydn-db promise has done, fail and them, etc, it is not $.Deferred instance. An adaptor approach is require.

Currently, use transaction as follow:

var results = {};
var tx_req = db.run(function(tx_db) {
  tx_db.count('items').done(function(x) {
    results.count = x;
  });
  tx_db.values('items').done(function(x) {
    results.values = x;
  });
  tx_db.get('config', 1).done(function(x) {
    results.config = x;
  });
}, ['items', 'config'], 'readonly');

req.done(function() {
  var count = results.count;
  var r = results.values;
  var config = results.config;
  var currency = config.currency;
  if(count > 0){
    var n = r.length;
    for (var i = 0; i < n; i++) {                   
      var id    = r[i].id;
      var itemId = r[i].itemId;
      console.log('ID: '+id+' itemID: '+itemId+' Currency: '+currency);
    }
  }
  results = null;
});

It is a bit messy, but more efficient because all three query run in a single transaction.

EDIT:

Just need to add promise() method that return an object having done, fail and progress functions. Should be doable without much overhead. Basically you can do an adaptor like:

var wrap = function(req) {
  req.promise = function() {
    return req; // Note: req has done, fail and progress functions.
    // however, jquery api demand promise to return a new deferred. 
  }
  return req;
}
$.when(wrap(db.count('items')),wrap(db.values('items')),wrap(db.get('config', 1)));

Here is complete code in jsfiddle.

EDIT:

From release 0.8.1, promise method is added to request object and wrapping is no longer needed. Example.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • Should I put `$.when` inside a variable and then use it as normal, like `var req = $.when(***);` and then use it like `req.done(function(count,r,config){****})`? thanks again for your help – Crash Override Oct 01 '13 at 19:22
  • No. we cannot resolve run request promise. Its result is not defined. – Kyaw Tun Oct 02 '13 at 01:33
  • I couldn't test the first solution yet, but I'll do it when I get home, another question, if I have this records in a table `db.add('person',{id:1,name:"Frank",age:23});` and then want to update a single record `db.put('person',{id:1,age:20});` the record `name:"Frank"` is deleted from table `person`. how can I update a single field without affecting others? thanks – Crash Override Oct 02 '13 at 16:05
  • could you ask in separate question? – Kyaw Tun Oct 03 '13 at 09:19