Reputation: 25032
I have created a service
to hold all of my search parameters. This way I can call my $resource
from anywhere I need and the search parameters are always correct. Here is the service
App.factory('filterService', function () {
var FilterService = {
actorId: '0001234',
actionStatus: ['BUILDING', 'STARTED', 'SENT'],
payer: null,
limit: 10,
offset: 0,
sortBy: null,
filterBy: null,
actionType: ['EB', 'AUTH'],
actorType: 'REQUESTING'
};
return FilterService;
});
So when I call the my REST service I can access the search criteria like this:
App.factory('encounterService', function ($resource, filterService) {
return {
resource: $resource('/internal/v2/encounters/:encounterId', {encounterId:'@encounterId'}, {
search: {
method: 'GET',
headers: {
'RemoteUser': 'jhornsby',
'Content-Type': 'application/json'
},
params: {
actorType: filterService.actorType,
actorId: filterService.actorId,
actionStatus: filterService.actionStatus,
actionType: filterService.actionType,
limit: filterService.limit,
offset: filterService.offset,
payer: filterService.payer,
filterBy: filterService.filterBy,
sortBy: filterService.sortBy
}
};
}
});
So I then call my search
like this:
encounterService.resource.search({}, function(data) {
// do something with data
});
But when I update my filterService
, from a controller
, it doesn't update my filterService'. Here is an example of the
filterService` being updated:
$scope.ok = function() {
// Access the filter items with this.model
filterService.actorId = $scope.model.actorId;
filterService.actionStatus = $scope.model.actionStatus;
filterService.actionType = $scope.model.actionType;
filterService.limit = $scope.model.limit;
}
Anytime I call search
, it is always using the defaults. How do I can I turn my search params into an object like I am trying to do here?
Upvotes: 0
Views: 65
Reputation: 1591
The problem is that the return statement inside your encounterService
only gets called once, when the module is initialised. The params
field on the search object gets evaluated then and so the values that the filterService
has at that point in time are the values the params
field is stuck with. In order to get around this, you need a way to evaluate the params object each time you want to make a query. A simple way to do this would be to return a function that you can call to get the resource, rather than the resource itself.
e.g.
App.factory('encounterService', function ($resource, filterService) {
return {
getResource: function () {
return $resource('/internal/v2/encounters/:encounterId', {encounterId:'@encounterId'}, {
search: {
method: 'GET',
headers: {
'RemoteUser': 'jhornsby',
'Content-Type': 'application/json'
},
params: {
actorType: filterService.actorType,
actorId: filterService.actorId,
actionStatus: filterService.actionStatus,
actionType: filterService.actionType,
limit: filterService.limit,
offset: filterService.offset,
payer: filterService.payer,
filterBy: filterService.filterBy,
sortBy: filterService.sortBy
}
}
};
}
}});
Which you can then call like this:
encounterService.getResource().search({}, function(data) {
// do something with data
});
Upvotes: 2
Reputation: 77904
I would use filterService
like OO object with getter ans setter. By this way will be easy to handle your service object and easy to understand the code after.
Something like:
App.factory('filterService', function () {
this.FilterService = {
actorId: '0001234',
actionStatus: ['BUILDING', 'STARTED', 'SENT'],
payer: null,
limit: 10,
offset: 0,
sortBy: null,
filterBy: null,
actionType: ['EB', 'AUTH'],
actorType: 'REQUESTING'
};
this.getFilterService(){
return this.FilterService;
}
this.setFilterService(service){
this.FilterService = service;
}
});
Now from controller we can call set:
filterService.setFilterService(dummyService);
and call get:
$scope.theService = filterService.getFilterService();
Hope it will work,
Upvotes: 0