2010-11-25 29 views
4

我有复制在CouchDB中工作,并且想要在更改推送到目标数据库时更新我的​​UI。我读过关于_changes数据库API的信息,并在jquery.couch.js中找到了couch.app.db.changes()函数。但是我无法弄清楚如何使用该函数。我假设我需要设置监听器,但是我对Javascript的知识还不是它所需要的。CouchDB _changes通知-jquery.couch.js couch.app.db.changes()用法

不幸的是,文档http://www.couch.io/page/library-jquery-couch-js-database甚至没有列出changes()函数。

有人可以帮助我在这里,也让我知道什么选项帕拉姆是。

这里是有问题的函数的代码:关于使用jQuery的Ajax的feateures

changes: function(since, options) { 
     options = options || {}; 
     // set up the promise object within a closure for this handler 
     var timeout = 100, db = this, active = true, 
     listeners = [], 
     promise = { 
     onChange : function(fun) { 
      listeners.push(fun); 
     }, 
     stop : function() { 
      active = false; 
     } 
     }; 
     // call each listener when there is a change 
     function triggerListeners(resp) { 
     $.each(listeners, function() { 
      this(resp); 
     }); 
     }; 
     // when there is a change, call any listeners, then check for another change 
     options.success = function(resp) { 
     timeout = 100; 
     if (active) { 
      since = resp.last_seq; 
      triggerListeners(resp); 
      getChangesSince(); 
     }; 
     }; 
     options.error = function() { 
     if (active) { 
      setTimeout(getChangesSince, timeout); 
      timeout = timeout * 2; 
     } 
     }; 
     // actually make the changes request 
     function getChangesSince() { 
     var opts = $.extend({heartbeat : 10 * 1000}, options, { 
      feed : "longpoll", 
      since : since 
     }); 
     ajax(
      {url: db.uri + "_changes"+encodeOptions(opts)}, 
      options, 
      "Error connecting to "+db.uri+"/_changes." 
     ); 
     } 
     // start the first request 
     if (since) { 
     getChangesSince(); 
     } else { 
     db.info({ 
      success : function(info) { 
      since = info.update_seq; 
      getChangesSince(); 
      } 
     }); 
     } 
     return promise; 
    }, 

回答

3

或者,您可以使用longpoll更改提要。这里有一个例子:

function bind_db_changes(database, callback) { 
     $.getJSON("/" + database, function(db) { 
      $.getJSON("/"+ database + 
         "/_changes?since="+ db.update_seq +"&heartbeat=10000&feed=longpoll", 
      function(changes) { 
       if($.isFunction(callback)){ 
        callback.call(this, changes); 
        bind_db_changes(database, callback); 
       } 
      }); 
     }); 
    }; 

    bind_db_changes("test", function(changes){ 
     $('ul').append("<li>"+ changes.last_seq +"</li>"); 
    }); 
+0

感谢koleto,这工作,以及我一直在做的其他工作,我能够解决如何使用couch.app.db.changes()函数。对不起,迟到的回应,但我不知道有另一个答案张贴。 – nevf 2010-12-04 03:05:06

0

什么?

function get_changes() { 
      $.getJSON("/path/to/_changes", function(changes) { 
       $.each(changes, function() { 
        $("<li>").html(this.text).prependTo(mychanges_div); 
       }); 
       get_changes(); 
      }); 
} 
setTimeout(get_changes, 1000); 
+0

谢谢,我可以试试这个,但是我只是试图使用CouchApps提供的代码。哦,对于一些文件或神老式的代码评论。 – nevf 2010-11-26 00:26:05

0

我一直在与JS Promises代码,这使得mt了解我上面张贴的CounchDB代码的工作。下面是一个示例:

var promise_changes = app.db.changes(); 
// Add our deferred callback function. We can add as many of these as we want. 
promise_changes.onChange(db_changes); 

// called whenever this db changes. 
function db_changes(resp) { 
    console.log("db_changes: ", resp); 
} 

谷歌浏览器进入繁忙状态与长轮询,我希望他们能解决的一天。