Reputation: 413
I'm trying to use my variable 'selectedDate' in my rest calls, but it always uses the old one, even after setting a new value with the 'setSelectedDate()' function
app.factory('MyService', function ($resource) {
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var selectedDate = curr_month + "/" + curr_date + "/" + curr_year;
return {
getSelectedDate : function(){
return selectedDate;
},
setSelectedDate : function(newDate){
selectedDate = newDate;
console.log(selectedDate); // My setSelectedDate works well because I can see the new value in the console
},
// The selectedDate always return the first value (the new Date() ), and not the updated date
Meal: $resource('/rest/food/meal', {date : selectedDate, mealId: '@mealId'}, {
query: {
method: 'GET',
isArray: true,
params : {date : selectedDate}
}
}),
}
});
It does work if I manually set the parameters like this, for example in my controller :
FoodService.setSelectedDate(newDate);
$scope.meals = FoodService.Meal.query(FoodService.getSelectedDate());
But I would like my service to automatically use the 'selectedDate' variable, and just have to call :
FoodService.setSelectedDate(newDate);
$scope.meals = FoodService.Meal.query();
But it always use the old (first) date and not the updated one..
Any ideas ?
EDIT :
I tried as params, and in the $resource after '/rest/food/meal' the following : - {date : getSelectedDate} => ReferenceError: getSelectedDate is not defined - {date : getSelectedDate()} => same - {date : this.getSelectedDate()} => TypeError: Object # has no method 'getSelectedDate'
And finally the one which should work.. -{date : selectedDate)} => Does work but always use the first selectedDate I defined in the first place, but not the updated date
Upvotes: 2
Views: 1394
Reputation: 50245
Factory needs to be modified as below:
app.factory('FoodService', function ($resource) {
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var selectedDate = curr_month + "/" + curr_date + "/" + curr_year;
function blahBlahFunction() {
return selectedDate;
}
return {
getSelectedDate : function(){
return blahBlahFunction();
},
setSelectedDate : function(newDate){
selectedDate = newDate;
console.log(selectedDate);
},
Meal: $resource('/rest/food/meal', {date : blahBlahFunction,
mealId: '@mealId'}, {
query: {
method: 'GET',
isArray: true,
params : { date : blahBlahFunction }
}
}),
}
});
Note:
params is the default pre-bound parameters. In order to provide parameters dynamically that element has to be wrapped in a function and the function to be used as param.
This almost defies the notion of default param, I think without the above changes the call would have been as:
FoodService.Meal.query({date: "12/11/2014"});
Refer this PLUNKER for details. Look for an error with the URL in console in dev tool. Should say:
GET http://run.plnkr.co/rest/food/meal?date=12%2F11%2F2014 404 (Not Found)
Upvotes: 1