diff options
-rw-r--r-- | share/www/script/jquery.couch.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/share/www/script/jquery.couch.js b/share/www/script/jquery.couch.js index 6d136c6b..32eeef5f 100644 --- a/share/www/script/jquery.couch.js +++ b/share/www/script/jquery.couch.js @@ -230,6 +230,51 @@ "Database information could not be retrieved" ); }, + changes: function(since, options) { + options = {} || options; + // set up the promise object within a closure for this handler + var 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) { + if (active) { + var seq = resp.last_seq; + triggerListeners(resp); + getChangesSince(seq); + }; + }; + // actually make the changes request + function getChangesSince(seq) { + ajax( + {url: db.uri + "_changes?feed=longpoll&since="+seq}, + options, + "Error connecting to "+db.uri+"/_changes." + ); + } + // start the first request + if (since) { + getChangesSince(since); + } else { + db.info({ + success : function(info) { + getChangesSince(info.update_seq); + } + }); + } + return promise; + }, allDocs: function(options) { var type = "GET"; var data = null; |