[SOLVED] Can I use 'path' in async routes 'resolve?'


#1

Hey Team,
Kindly guide me here.
documentation says:

resolve(parameters, options) parameters object - object with resolved route content. Must contain one of url, content, template, templateUrl, component or componentUrl properties

PROBLEM
I have an async route, after the request is made successfully, I check the response, if it is some_response, I simply resolve and redirect to some template.
However if response is some_different_response, I need to pass a few more query strings to a path and make one more request based on which I can finally resolve a different template now!

See this:

  `path: 'check-for-something',
    async(routeTo, routeFrom, resolve, reject) {
      app.request({
      ....
      success: function(data){
                if (data.some_response) {
               resolve({ url: 'secured.html' })
      } else if(data.some_different_response) {
           //Make one more request here before resolving
          resolve({path: /second-request/:some_query_string });
      }
              }
       })`

How possible is that? or is there anyother alternative? Thanks a lot
@nolimits4web


#2

use “componentUrl”

E.g.

path: '/my-page/'
async(routeTo, routeFrom, resolve, reject) {
app.request({
....
if (data.some_response == 1) {
         resolve(
			  {
				componentUrl: './pages/my-new-shirt.html',
			  },
			  {
				context: {
				  text: 'This page about my new shirt',
				  cache: false,
				  xhrCache: false,
				 }
			  }
		);
} else {
        // You can make second request here and get response
        // and then on basis of response, you can again do if-else-condition as above or simply resolve
        resolve(
			  {
				componentUrl: './pages/my-old-shirt.html',
			  },
			  {
				context: {
				  text: 'This page about my old shirt',
				  cache: false,
				  xhrCache: false,
				}
			  }
			);
}

On html template pages, you can do {{text}} to get your content. And make sure those html pages have <template> ... your page content ... </template> tag


#3

@B.Alam, thanks a lot. But my confusion isn’t resolving. The main confusion is making a second request while passing a querystring on that path.
The URL I am supposed to redirect to in my second request is already declared above in routes. Like path: '/some page/:some_param/'

So in other words, in my else condition, I want to redirect to some route where I perform a request and finally redirecting the user based on that response…

It’s already being used, I just want to redirect the user to their progress at login… Is it more clear now?

So in other words, I need something that works like this in async routes rosolve…

app.navigate()...


#4

Why you are declaring path: again i second request. I believe this will not work. If you know values of those “some_query_string” then you can pass those values in second request. for e.g -

} else {
    var string = 'some_query_string';
    app.request.post('http://localhost/query.php', 
		{ string: string }, 
		function (response) {
                       .......
                }
    }

Sorry, if i understand wrong again.

I think I can help if I get clear picture of what you actually want.


#5

@B.Alam, thanks very much for this. Let me go in detail exactly what I really want to do. In my app, it’s sort of progressive, the way it works. there are steps in there. Once a user completes one step, they are redirected to some other. These steps build on each other and we rely on requests to some external MYSQL DB hosted. The app has an already established web-app (system) that is already working! I am basically connecting to the same databse just so everything is in sync.

So, At login, if the email matches what we have in the db, then I go ahead and query the database to track your progress and return it with a full response in JSON because I would want to redirect users to their specific page of progress. Forexample, users make an order, if they are responded to by some admin, they are taken to some page to make payment. So I have to check at login, just so I know where to send them exactly.

But remember, if they stay logged in, they keep getting redirected of course. So I already have those routes declared.

path: '/something/:param', async: function (routeTo, routeFrom, resolve, reject) { }
So… at login, if I receive response showing progress and users are supposed to be seeing data made from request done in the route path: /something/:param/,above, then instead of repeating the same request here (repeating myself by writing this ame code), can’t I just redirect them to path: /something/:param/ and get their final redirection from there? That is all I was asking… Is it clear now?


#6

You can’t use path in resolve, all you can do is to prevent route loading and load another route, e.g.:

async: function (to, from, resolve, reject) {
  var router = this;
  if (match) {
    resolve({url: 'secured.html'});
  } else {
    reject();
    router.navigate('/some-other-route/');
  }
}

#7

Wow! Vlad, you’re such a blessing! Exactly what I wanted!!! This is solved! Thank you very much @nolimits4web