Resolución de promesas en la dependencia del servicio

I develop client side with AngularJS for Rails application and I stuck with authenticity_token.

Here is how my service looks like:

blog_app.factory('AuthenticityToken', ['$http', '$q',
  ($http, $q) ->
    () ->
      delay = $q.defer()
      $http.get('/authenticity_token').success(
        (data) ->
          delay.resolve(data.authenticity_token)
      ).error(
        ->
          delay.reject('Can\'t get authenticity_token')
      )

      return delay.promise
])


post_services.factory('Post', ['$resource',
  ($resource) ->
    $resource('/posts/:id', {id: '@id'},
    {
      query: {
        method: 'GET',
        params: {limit: 10, order: 'desk'},
        isArray: false
      }
    }
    )
])

¿Cómo puedo hacer AuthenticityToken be injected and resolved in Publicación, so I could use authenticity token in Publicación's default params?

Instead I can only resolve it for controller:

angular.module('posts', ['posts.services']).config(
  ['$routeProvider', ($routeProvider) ->
    $routeProvider
    .when('/posts', {
        templateUrl: '/pages/posts/',
        controller: 'ListCtrl',
        resolve: {
          authenticity_token: ['AuthenticityToken', (AuthenticityToken) ->
            AuthenticityToken()
          ]
        }
    })
  ]
)

And then pass to Publicación resource explicitly

preguntado el 13 de septiembre de 13 a las 11:09

1 Respuestas

angular.module('posts', ['posts.services']).config(
  ['$routeProvider', '$httpProvider', ($routeProvider, $httpProvider) ->
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/json'
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/json'
    token = $("meta[name='csrf-token']").attr("content")
    $httpProvider.defaults.headers.post['X-CSRF-Token'] = token
    $httpProvider.defaults.headers.put['X-CSRF-Token'] = token
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = token

    $routeProvider
    .when('/posts', {
        templateUrl: '/pages/posts/',
        controller: 'ListCtrl'
    })
  ]
)

Respondido el 13 de Septiembre de 13 a las 11:09

I ask you about promise resolution, in place of AuthenticityToken service may be another one, and I need to resolve promise before injection - átomoAltera

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.