I am building an AngularJS app backed by a third-party API. I have a basic resource like this:
module.factory('UserPreference', function($resource) {
return $resource('/users/:user_id/preferences/:preference_type_id', {
user_id: "@user_id",
preference_type_id: "@preference_type_id"
})
})
If the user has not specified a preference for a certain preference type, the result will be null. I want to initialize the object in the case that the response is null.
{user_id: 1, preference_type_id: 1, preference: null}
I can see two options:
- Do this in transformResponse within the factory
- Handle it in the controller
1 seems like the cleaner option since this is really a data issue more than a controller issue, but I can't figure out the details of how to do that. Specifically, I can't access the :user_id and :preference_type_id params that get passed to the resource.
Should I just handle this in the controller? Is there something really obvious I'm missing?
Wrap it on a function that you can call as many times as you like.