javascript - Reject deferred in $update function of an angular $resource -


in angular application update task follows

task.$update().then(function (task) {    // success }, function (response) {    // failure }); 

from backend 422 back, first callback called.

my resource looks this

angular.module('app')     .factory('task', function ($resource) {         var resource = $resource('/admin/tasks/:id', { id: '@id' }, {             new: {                  method: 'get',                  url: '/admin/tasks/new'             },             update: {                  method: 'put'             },             ...         });      return resource; }); 

the question is, under circumstances second callback called ? , if needed, can in update method such second callback called?

update: reason success callback called time because of error-interceptor have

angular .module('app')     .factory('errorinterceptor', function ($location, $q, $rootscope) {         return {              responseerror: function (response) {                 if (response.config.url.match($location.$$host)) {                     $rootscope.error = response;                      return $q.reject(response);                 }                  return $q.when(response);             }          };     }); 

apparently, if return $q.when(response); tell angular fine, right ?

the callbacks specified params $update() functions, this:

$task.$update(function(task) {}, function(response) {}) 

see official documentation of $resource :

the action methods on class object or instance object can invoked following parameters:

  • http "class" actions: resource.action([parameters], [success], [error])
  • non-get "class" actions: resource.action([parameters], postdata, [success], [error])
  • non-get instance actions: instance.$action([parameters], [success], [error])

the thing unlike $http, $update or resource methods don't return promise directly. return empty reference.

note $resource uses $http internally, hence $http docs apply (regarding callbacks) :

a response status code between 200 , 299 considered success status , result in success callback being called. note if response redirect, xmlhttprequest transparently follow it, meaning error callback not called such responses.


Comments