1

I'm trying to build a logging system for events on a number of various $resource. I have a factory that I've built that does exactly what I would like, however, I don't want to pollute my controllers with unnecessary code, i'd like to get the logger factory to trigger every time a resource is triggered (being able to customize the data being sent to the factory).

I can't seem to find a way to do this through the various methods on the $resource.

Any ideas or suggestions for solving this type of problem? Am I overcomplicating it?

scniro
  • 16,844
  • 8
  • 62
  • 106
flashpunk
  • 772
  • 2
  • 13
  • 38

1 Answers1

0

Think interceptors would be the way to go. Here's a sample we're using to add a token to all requests being made:

.factory 'TokenInterceptor', ($q, $window, AuthenticationService) ->
return {
    request:  (config) ->
        config.headers = config.headers || {}
        if $window.sessionStorage.username 
            config.headers.Authorization = $window.sessionStorage.token
        config 
    response:  (response) ->
        response || $q.when(response)
}

.config ($httpProvider) ->
    $httpProvider.interceptors.push('TokenInterceptor')

As you can see, we add the factory to the interceptors, which is basically what you want to do I think.

Alberto
  • 880
  • 5
  • 15