You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2010/06/25 16:11:25 UTC

svn commit: r957956 - /couchdb/branches/0.11.x/share/www/script/jquery.couch.js

Author: jan
Date: Fri Jun 25 14:11:25 2010
New Revision: 957956

URL: http://svn.apache.org/viewvc?rev=957956&view=rev
Log:
Merge r954560 from trunk:

add changes handler to jquery.couch.js

Modified:
    couchdb/branches/0.11.x/share/www/script/jquery.couch.js

Modified: couchdb/branches/0.11.x/share/www/script/jquery.couch.js
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/share/www/script/jquery.couch.js?rev=957956&r1=957955&r2=957956&view=diff
==============================================================================
--- couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] (original)
+++ couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] Fri Jun 25 14:11:25 2010
@@ -212,6 +212,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) {
           ajax(
             {url: this.uri + "_all_docs" + encodeOptions(options)},