javascript - How store AngularJS $resource calls in an array to execute all together after? -
i'm working angularjs 1.5 (i'm beginner js) in user profile view user can change lot of things in data.
in view data split in several sections , save of data means several calls server ( due design of app). problem found when user modify data maybe modify part of , when push save button don't want call methods, necessary methods.
i've programed way detect changes in data blocks when user push save button, controller make call , in other cases 2 or three. problem calls (made $resource library) executed asyncronously , can control better.
i next: store calls in list or array wihout execute them , after execute @ same time (more or less). if of fails show error message user (only generic error message), internally log call failed, , in same way show (and one) success message when calls have ended success ( , not message per success call).
i don't know how this, mates me maybe need use $q angularjs service this, or store promises $resource have execute after (i've trying without success) or work promises in js.
anyone can give me idea?
finally resolved problem using $q. @ first wanted store calls without execute them (i thought better) can check stored results of calls enought aim. so, skeleton of solution i've been done:
at beginning of controller
var promises = []; in places need make controlled call inside of save user data function:
var deferred = $q.defer(); var promise = vm.teacher.$update( function () { // success deferred.resolve('success updating teacher.'); }, function (error) { // fail deferred.reject('error updating teacher, error: ' + error) }); promises.push(deferred.promise) } ... ... vm.otheritems.$update ... ... and @ end of function, this:
$q.all(promises).then( function(value){ console.log('resolving promises, success, value: ') console.log(value); toastservice.showtoast('success updating teacher.'); // deleted old promises next iteration (if exists) promises = []; },function(reason){ console.log('resolving promises, fail, reason: ') console.log(reason); toastservice.showtoast('fail updating teacher.'); } ) thanks help!
Comments
Post a Comment