couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [20/50] jquery-couch commit: updated refs/heads/import-master to 66dbcbf
Date Tue, 01 Apr 2014 08:52:25 GMT
add changes handler to jquery.couch.js

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@954560 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/couchdb-jquery-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-jquery-couch/commit/39550380
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-jquery-couch/tree/39550380
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-jquery-couch/diff/39550380

Branch: refs/heads/import-master
Commit: 3955038067f89391590a3750d4260e9dda42b1d7
Parents: 22aa6b2
Author: John Christopher Anderson <jchris@apache.org>
Authored: Mon Jun 14 17:13:19 2010 +0000
Committer: Alexander Shorin <kxepal@gmail.com>
Committed: Mon Jun 14 17:13:19 2010 +0000

----------------------------------------------------------------------
 jquery.couch.js | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-jquery-couch/blob/39550380/jquery.couch.js
----------------------------------------------------------------------
diff --git a/jquery.couch.js b/jquery.couch.js
index 126aac0..086ebbb 100644
--- a/jquery.couch.js
+++ b/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;


Mime
View raw message