Hi. I can give you my code that you can apply to your case.
I have created a factory to access Couchbase:
.factory('CouchBase', ['$window',function($window) {
var couchbase = {}
//load sites list
couchbase.refreshSitesView = function() {
var options = {
descending : false
}
return divePlanDatabase.queryView("sites",options)
}
return couchbase;
}
in the config I have defined the views and the divePlanDatabase general variable:
this.diveplanViews =
{
sites : {
map : function(doc, meta)
{
if (doc.type == "site" && doc.name && doc.geography && doc.geography.country) {
emit([doc.geography.country,doc.name],{
name : doc.name,
lat : doc.geography.latitude,
lng : doc.geography.longitude
})
}
}.toString()
}
And finally in the view controller you can call a function that can be refreshed everytime the database receives some update:
function refreshSites() {
CouchBase.refreshSitesView().then(function(sitesList) {
$scope.sites = sitesList.rows;
}
}
refreshSites()
//refresh on database change
$rootScope.$on("couchbase:change:site", function(event) {
refreshSites()
});
$rootScope.$on("couchbase:deleted", function(event) {
refreshSites()
});
This is done using ngCouchbaseLite with a little tweak that sends a:
$rootScope.$broadcast("couchbase:change:" + doc.type);
from within the “listen” function.