具有多个延期的YDN-DB [英] YDN-DB with multiple deferred

查看:103
本文介绍了具有多个延期的YDN-DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对jquery $ .ww使用多个延迟,但到目前为止还算不上运气,这是我的代码:

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

推荐答案

我明白了.我将看到如何实现jquery延迟列表.尽管ydn-db promise具有donefailthem等,但它不是$.Deferred实例.需要适配器方法.

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.

只需添加 promise()方法返回具有donefailprogress函数的对象.应该是没有太多开销的可行的.基本上,您可以像这样做一个适配器:

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)));

这是jsfiddle中的完整代码.

Here is complete code in jsfiddle.

从版本0.8.1开始,将promise方法添加到请求对象,并且不再需要包装. 示例.

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

这篇关于具有多个延期的YDN-DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆