首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CouchDB _changes通知- jquery.couch.js couch.app.db.changes()使用率

CouchDB _changes通知- jquery.couch.js couch.app.db.changes()使用率
EN

Stack Overflow用户
提问于 2010-11-25 09:55:13
回答 4查看 2.5K关注 0票数 4

我可以在CouchDB中进行复制,并希望在将更改推送到目标数据库时更新我的UI。我读过有关_changes数据库应用程序接口的文章,并在jquery.couch.js中找到了couch.app.db.changes()函数,但是我不知道如何使用该函数。我假设我需要设置listener,但我对Javascript的了解还不够。

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

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

下面是有问题的函数的代码:

代码语言:javascript
复制
    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;
    },
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-29 04:32:22

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

代码语言:javascript
复制
    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>");
    });
票数 3
EN

Stack Overflow用户

发布于 2012-05-04 21:16:39

请注意,$.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
EN

Stack Overflow用户

发布于 2010-11-26 07:15:35

使用jquery的ajax特性怎么样?

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4273140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档