Something along these lines would probably do it, just edit the semantics to match your program.
In your controller:
var getList = function() {
$scope.comments = CommentService.list();
};
CommentService.registerObserverCallback(getList);
And in your service:
var observerCallbacks = [];
// register the observer's callback in our callback list.
this.registerObserverCallback = function(callback){
observerCallbacks.push(callback);
};
// function to notify our observers and call the callback they registered with
var notifyObservers = function(){
angular.forEach(observerCallbacks, function(callback){
callback();
});
};
this.post = function(comment) {
return $http.post('/api/v1/comments/', comment).success(function(data, status, headers, config) {
notifyObservers();
})
};
So basically your controller tells your service, "hey, I'm observing you.. If you do anything, call this call back function", and passes it getList
. The service registers the observer, and calls the callback after the post()
returns successful.