1

What I mean by resolve in a resolve is that I want to be able to resolve the user first, and then resolve all his additional info, eg, addressess, contacts, friends.

What I currently have:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    return Auth.requestUser();
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

What I'll like to do is the following:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    Auth.requestUser().then(function(){
                       // HERE I WANT TO RESOLVE OTHER RESOURCES
                       // WHAT WILL I RETURN HERE?
                    });
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

I'm not sure if this is possible.

1

2 Answers 2

2

You can inject "resolved" parameters into other resolve functions:

resolve: {
  current_user: ['Auth', function(Auth){
    return Auth.requestUser();
  }],

  // "Svc" here is some service I assume you have to get friends, address, etc...
  friends: ["Svc", "current_user", function(Svc, current_user){
     // Svc.getFriends can return a promise or a value
     return Svc.getFriends(current_user);
  }],

  address: ["Svc", "current_user", function(Svc, current_user){
     return Svc.getAddress(current_user);
  }]
}

Then, in the controller for that state, you can inject current_user, friends, and address as parameters:

.controller("WebsiteMainViewCtrl", function($scope, current_user, friends, address){
   // ...
})
Sign up to request clarification or add additional context in comments.

2 Comments

this one is better and clean
Perfect, this is pretty neat!
2

You can do something like this.

 resolve : {
       // Request the current signed in user >
       current_user : ['Auth', function(Auth){
          var deferred = $q.defer();
          Auth.requestUser().then(function(){
              // Call this deferred.resolve in inner asyc callback
              deferred.resolve(data);
          });
          return deferred.promise;

       }]
 }

Or

You can also use chaining

  resolve : {
       // Request the current signed in user >
       current_user : ['Auth', function(Auth, userInformation){
          var authData;
          return Auth
              .requestUser()
              .then(function (_authData) {
                  // do something with dataFromAuth
                  authData = _authData;
                  return userInformation.getDetailInfoAboutUser(_authData.userid); 
              })
              .then(function (detailedUserInfoObject) {
                  // do something with dataFromOtherResource and return
                  detailedUserInfoObject.authInfo = authData;
                  return detailedUserInfoObject;
              });
       }]
 }

11 Comments

While this will fetch all the "other" data, it will also assign dataFromOtherResouce to the injectable current_user, which seems counter-intuitive
OtherResource(dataFromAuth.something); will return promise. so the last then function will not get called until OtherResource(dataFromAuth.something);'s returned promise is not resolved.
I understand, but then you do return dataFromOtherResource; - this will assign dataFromOtherResource to the now resolved current_user
But, you can manipulate dataFromOtherResource before returning it. and if you want use the dataFromAuth value inside last function then you can simply store it in a variable in second then function
Ok, but what if I want both the user and the other data - which is what the OP requested? You could definitely return an object with all the data you collected: {user: dataFromAuth.user, other: dataFromOtherResouce}, sure... sub-optimal, in my opinion, but works... But modify your answer to show that, since right now it assigns unrelated data to current_user parameter
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.