diff --git a/angular-pouchdb.coffee b/angular-pouchdb.coffee index 6490af4..687e1b7 100644 --- a/angular-pouchdb.coffee +++ b/angular-pouchdb.coffee @@ -86,11 +86,18 @@ pouchdb.provider 'pouchdb', -> bulkDocs: qify db.bulkDocs.bind(db) allDocs: qify db.allDocs.bind(db) changes: (options) -> - clone = angular.copy options - clone.onChange = (change) -> - $rootScope.$apply () -> - options.onChange change - db.changes clone + changes = db.changes options + deferred = $q.defer() + changes.on 'change', (change) -> + $timeout () -> + deferred.notify {change: change, changes: changes} + changes.on 'complete', (res) -> + $timeout () -> + deferred.resolve res + changes.on 'err', (err) -> + $timeout () -> + deferred.reject(err) + deferred.promise putAttachment: qify db.putAttachment.bind(db) getAttachment: qify db.getAttachment.bind(db) removeAttachment: qify db.removeAttachment.bind(db) diff --git a/angular-pouchdb.js b/angular-pouchdb.js index 385592b..1aadc58 100644 --- a/angular-pouchdb.js +++ b/angular-pouchdb.js @@ -108,14 +108,28 @@ THE SOFTWARE. bulkDocs: qify(db.bulkDocs.bind(db)), allDocs: qify(db.allDocs.bind(db)), changes: function(options) { - var clone; - clone = angular.copy(options); - clone.onChange = function(change) { - return $rootScope.$apply(function() { - return options.onChange(change); + var changes, deferred; + changes = db.changes(options); + deferred = $q.defer(); + changes.on('change', function(change) { + return $timeout(function() { + return deferred.notify({ + change: change, + changes: changes + }); }); - }; - return db.changes(clone); + }); + changes.on('complete', function(res) { + return $timeout(function() { + return deferred.resolve(res); + }); + }); + changes.on('err', function(err) { + return $timeout(function() { + return deferred.reject(err); + }); + }); + return deferred.promise; }, putAttachment: qify(db.putAttachment.bind(db)), getAttachment: qify(db.getAttachment.bind(db)),