Reputation: 1509
I am trying to build an app , in which I have a list of data's for each date's . I have the data
stored under date
in string
format . So I have created a factory
.factory('CustomResource',function(UserService,$firebase){
var ref=new Firebase("https:/<url>/days/'+UserService.today.toDateString()+'/data');
return $firebase(ref);
});
and in the controller I have set a watch on the date variable that is changed by click or calendar .
$scope.$watch('today',function(d){
CustomResource.$asArray().$loaded().then(function (eee) {
$scope.eeees = eee;
});
The first time the app is loaded all the data loads as expected . But when I change the date $firebase is not triggered . I know it's because the data is stored locally , But what is a workaround . ? Please Help .
Thank You
Upvotes: 0
Views: 61
Reputation: 7958
Factories are singletons so they only get created once. In case the date changes you should recreate the firebase object. I'd also recommend putting the firebase load code in the factory itself so it can be re-used and it also separates the data loading code from your controller
app.factory('CustomResource',function(UserService,$firebase){
var getData = function () {
var ref=new Firebase('https:/<url>/days/'+UserService.today.toDateString()+'/data');
return $firebase(ref).$asArray().$loaded();
};
return {
getData: getData
};
});
Then you can use it like:
$scope.$watch('today',function(d){
CustomResource.getData().then(function (exp) {
$scope.expenses = exp;
});
});
I use this as a guide for using data services: https://github.com/johnpapa/angularjs-styleguide#data-services
Upvotes: 3