Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created February 17, 2014 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwormald/9044480 to your computer and use it in GitHub Desktop.
Save robwormald/9044480 to your computer and use it in GitHub Desktop.
.factory('FavorStore',function($q,$firebase,FireBase,UserAuth,fireBaseUrl){
var deferredStore = $q.defer()
var user = UserAuth.getLoggedInUser()
user.then(function(user){
var userPath = 'users/' + user.id + '/favors'
var userStore = new Firebase(fireBaseUrl + userPath)
deferredStore.resolve($firebase(userStore))
})
return deferredStore.promise;
})
//inject the FavorStore service
.factory('FavorData',function(FavorStore){
//this is the Ref
var favors = FavorStore;
function _countFavors(){
//create promise
var deferredCount = $q.defer()
favors.once('value', function(snapshot) {
var count = 0;
snapshot.forEach(function(childSnapshot) {
count++;
});
deferredCount.resolve(count)
});
}
//public API
return {
getFavorCount : _countFavors
}
})
//in controller
FavorData.getFavorCount().then(function(count){
$scope.countedFavors = count;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment