0

I have a $watch attached to the appId variable. SO, when appId get changed it called the init function

$scope.$watch('appId', function() {                
    if ($scope.appId != '') {
        console.log('Inside appId watch');
        $scope.init();
    }
});

init() calls two service methods

$scope.init = function() {

    if ($scope.appId === undefined || $scope.appId == '') return false;

    $scope.getPrincipals($scope.loadPrincipals);
    $scope.getSignaturesByAppId($scope.loadSignatures);     

};

The methods are

$scope.getSignaturesByAppId = function (callback) {
    ApplicationDataSource.getSignaturesByAppId($scope.appId, function (result) {
        callback(result);
        $scope.$apply();
    });
};


$scope.loadSignatures = function (result) {
    var signatureResultSet = angular.fromJson(result[0]);
    $scope.appSignatures = signatureResultSet;

    if($scope.appSignatures.length===0){
        $scope.setDefaultValue();
    }
    else{
        $scope.setValueFromObject();
    }

};

$scope.getPrincipals = function (callback) {
    ApplicationDataSource.getApplicationPrincipalList($scope.appId, function (result) {
        callback(result);
        $scope.$apply();
    });
};

$scope.loadPrincipals = function (result) {
    var guarantorResultSet = angular.fromJson(result[0]);
    $scope.principals = guarantorResultSet;
};

The problem occurs here. In loadSignatures(), I have called a method setDefaultValue() which needs the data retrieve from loadPrincipals. So, when, loadSignatures called, principal data is not updated.

How to call the $scope.getPrincipals($scope.loadPrincipals) after $scope.getSignaturesByAppId($scope.loadSignatures) finish to retrieve data.

2 Answers 2

2

You can use Promises, here is an example:

var promise = callThatRunsInBackground();

promise.then(
  function(answer) {
    // do something
  },
  function(error) {
    // report something
  },
  function(progress) {
   // report progress
  });

So in your code it might look like (I will leave it to you to fix as I'm not going to compile or check for syntax errors):

var getPrincipals = $scope.getPrincipals($scope.loadPrincipals);

getPrincipals.then(
       function(datapayload) {  
            //do something with datapayload perhaps
            $scope.getSignaturesByAppId($scope.loadSignatures); 
     });

This will make it wait until getPrincipals is finished before running getSignaturesbyAppId

Sign up to request clarification or add additional context in comments.

3 Comments

It says, can not read property then of undefined. Why the getPrincipals is undefined here ?
i cannot comment on why it's not working for you right now without downloading your project and debugging it myself. however, with promises you're on the correct path to solving the issue you're experiencing
Here is some additional reading: chariotsolutions.com/blog/post/…
0

What solve my issue is, I called $scope.getSignaturesByAppId($scope.loadSignatures); in loadPrincipals callback function, not in init()

$scope.loadPrincipals = function (result) {
       var guarantorResultSet = angular.fromJson(result[0]);
       $scope.principals = guarantorResultSet;

       $scope.getSignaturesByAppId($scope.loadSignatures); 
};

2 Comments

Brilliant, glad you got it sorted. This is basically a promise. Apologies for not giving more examples. Without an Angular version I'm usually just able to give advice to the point where you finally solve the issue you had
No problem. Got the idea from your answer. Accepted the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.