Created
February 17, 2014 03:55
-
-
Save robwormald/9044480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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