我可以在CouchDB中进行复制,并希望在将更改推送到目标数据库时更新我的UI。我读过有关_changes数据库应用程序接口的文章,并在jquery.couch.js中找到了couch.app.db.changes()函数,但是我不知道如何使用该函数。我假设我需要设置listener,但我对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;
},发布于 2010-11-29 04:32:22
或者,您也可以使用longpoll changes提要。这里有一个例子:
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>");
});发布于 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
发布于 2010-11-26 07:15:35
使用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); https://stackoverflow.com/questions/4273140
复制相似问题