I have replication running in CouchDB and I want to update my interface when the changes are transferred to the target database. I read about the _changes database API and found the couch.app.db.changes () function in jquery.couch.js however I cannot decide how to use this function. I assume I need to set up a listener, but my knowledge of Javascript is not yet what it should be.
Unfortunately, the documents http://www.couch.io/page/library-jquery-couch-js-database do not even list the changes () function.
Can someone help me, and also let me know what the parameters are for.
Here is the code for the function in question:
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; },
source share