CouchDB _changes通知 - jquery.couch.js couch.app.db.changes()用法

时间:2010-11-25 01:55:13

标签: javascript jquery couchdb

我在CouchDB中进行复制,并希望在将更改推送到目标数据库时更新我的​​UI。我已经阅读了_changes数据库API并在jquery.couch.js中找到了couch.app.db.changes()函数但是我无法弄清楚如何使用该函数。我假设我需要设置监听器,但我对Javascript的了解还不是它需要的。

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

有人可以帮助我,也让我知道选项参数是什么。

以下是相关功能的代码:

    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;
    },

4 个答案:

答案 0 :(得分:3)

或者,您可以使用longpoll更改Feed。这是一个例子:

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

答案 1 :(得分:2)

请注意,$ .couch.db.changes现在位于官方文档中:

http://daleharvey.github.com/jquery.couch.js-docs/symbols/%24.couch.db.changes.html

这里也是一个使用jquery.couch插件消费_changes的好例子:

http://bradley-holt.com/2011/07/couchdb-jquery-plugin-reference

答案 2 :(得分:0)

如何使用jquery的ajax特性?

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

答案 3 :(得分: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 );
}

谷歌Chrome浏览器进入繁忙状态并进行长时间轮询,我希望有一天能够解决这个问题。