I have a form that submits to a 3rd-party service in order to receive a "token". I can specify a callback function that will handle the response from the 3rd-party service. Ideally, this response will be the token, however it could also be an error message. The callback function is in my controller. In that function, I set some other $scope variables that are required to move forward in the app's flow.
Since I cannot move forward without the updated $scope varible values and they are being set in my callback function, I think I'm stuck using either $watch to trigger some more events when the $scope variables are updated or I can put the rest of my functionality in the callback function.
Option 1 (simplified example):
use $watch on $scope variable to move forward when its value is updated
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.token = false;
$scope.$watch('token', function () {
doThis();
for(var i=0; i<len; i++) {
myFnction("Do Some", MORE_STUFF);
}
someObj.fetchGoodStuff($scope.token);
});
$scope.myCallback = function(status, response) {
if(!response.error) {
$scope.token = response.token;
}
})
}]);
Option 2 (simplified example):
move forward from within the callback function
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.token = false;
$scope.$watch('token', function () {
doThis();
for(var i=0; i<len; i++) {
myFnction("Do Some", MORE_STUFF);
}
someObj.fetchGoodStuff($scope.token);
});
$scope.myCallback = function(status, response) {
if(!response.error) {
$scope.token = response.token;
doThis();
for(var i=0; i<len; i++) {
myFnction("Do Some", MORE_STUFF);
}
someObj.fetchGoodStuff($scope.token);
}
})
}]);
To me, it seems more "correct" to isolate the base functionality, which in this case is receiving the response from a 3rd-party service, of the callback function and put the proceeding functionality elsewhere.
BUT the only other place I can see to put it is in a function called by $watch... and since the value of the $scope variable is only changing once per page visit, $watch does not seem appropriate here.
Does anyone have any insight regarding the best way to take action once a response is received?