4

I have a service holding all my API endpoint connectors. Those endpoint connectors are ngResource objects.

In some of them I override default methods, such as get or query for example to add responseTransformers. I'd like to be able to set timeout property of get method to stop one request before sending another, but I want to do it not in my service, where I define all $resources:

this.collections = $resource(BackendConfig.apiUrl + 'collections/:uid/', {}, {
    query: {
        isArray: false,
        method: 'GET',
        timeout: myTimeoutPromise
    },
});

but on runtime, just before I do this

Resource.collections.query().then(function () {});

Analysing code I came into conclusion, that it is possible to override params (2nd parameter), but not actions (3rd).

I found possible (not yet implemented) solution: create new service holding all timeouts (let's say Interrupter), inject it with my Resource service where I need it and define timeout promises there. Is there more convenient solution?

renczus
  • 170
  • 7
  • "service" way described above works poorly because when promise is once resolved it seems to block the use of method at all. – renczus Nov 06 '13 at 16:57
  • That's because once the promise is resolved, it stays permanently in that state. Thus, `collections.query()` won't work because AngularJS keeps thinking it's timed out. – cdmckay Mar 12 '14 at 02:21

1 Answers1

0

I don't know what your $resources do, but maybe you can chain your promises into a provider.

$http.get( SOME_URL_HERE ).
  then( function(response){
    /*Resolve the first promise then chain your other requests*/
    var promises = [ ];

    promises.push ( $http.get( OTHER_URL ) );
    promises.push ( $http.get( AGAIN_OTHER_URL ) );

    return $q.all( promises );
  })
  .then ( function( responses){
    /*This 'then' is of the chaining*/
    reponses[0].something /*response of the first promise*/
    reponses[1].something /*response of the second promise*/

  })
  .catch( function(error){
    console.log( error );
  });
eusoj
  • 374
  • 3
  • 12