javascript - service method does not recognize variable defined outside -
here service:
angular.module('core').factory('servererroralert', ['toaster', function(toaster) { return function(errorresponse) { toaster.pop('error', 'error', errorresponse); if (deferred) { deferred.reject(errorresponse); } } } ]);
here how call :
function update(updatedtransaction, originaltransaction, updatelocal) { var deferred = $q.defer(); updatedtransaction.$update(function() { if (updatelocal) {angular.extend(originaltransaction, updatedtransaction);} deferred.resolve(updatelocal ? originaltransaction : false); } , servererroralert); return deferred.promise; };
the second update function calling servererroralert service. return promise can chained.
as can see servererroralert merely convenience function. not define deferred in service, instead hoping recognize it. however, not case, deferred not recognized service.
i guess confused because if replace servererroralert argument in $update return value of service, namely function definition, confident recognize deferred. why not recognize once method extracted?
basically, want servererroralert handle server errors arising $resource calls not want keep copy , pasting function definition error callback. instead hoping paste 1 word in there (for code reuse).
how can accomplish trying accomplish?
thanks in advance.
simply return rejected promise, eg
.factory('servererroralert', ['$q', 'toaster', function($q, toaster) { return function(errorresponse) { toaster.pop('error', 'error', errorresponse); return $q.reject(errorresponse); } }]);
i change way you're resolving resource promise. don't need create own deferred object. try instead...
return updatedtransaction.$update().then(function() { if (updatelocal) { // assuming defined , in scope angular.extend(originaltransaction, updatedtransaction); return originaltransaction; } return false; }, servererroralert);
Comments
Post a Comment