gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:38:49 26 4
gpt4 key购买 nike

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

最佳答案

或者,您可以使用长轮询更改提要。这是一个例子:

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

关于javascript - CouchDB _changes 通知 - jquery.couch.js couch.app.db.changes() 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4273140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com