ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Support Promises as a result of function from the second parameter of $urlRouterProvider.when()

Open hyzhak opened this issue 13 years ago • 11 comments

Support Promises as a result of function from the second parameter of $urlRouterProvider.when().

Simplified example:

$urlRouterProvider.when('/location/', function() {
    return getPosition().then(function(value) {
        return 'venues/' + value.zoom + '/' + value.lat + '/' + value.lng;
    });
});

function getPosition() {
    var defer = $q.defer();
    defer.resolve('/location/1/2/3');
    return defer.promise;
}

hyzhak avatar Sep 03 '13 18:09 hyzhak

Is there a reason this couldn't be handled in a rule, or a state's resolve property?

jmstout avatar Sep 03 '13 20:09 jmstout

Yes, It's can. But it's look weird.

For example:

$stateProvider.state('getLocation', {
    url: '/location/',
    controller: function($state) {
        getPosition().then(function(value) {
            $state.go('venues', {zoom: value.zoom, lat: value.lat, lng: value.lng});
        });
    }
});

And I think it will be much better if $urlRouterProvider support that. Because redirection is area of ​​responsibility of $urlRouterProvider.

hyzhak avatar Sep 03 '13 20:09 hyzhak

It was my impression that the implementation of $urlRouterProvider was meant to be light? Someone from the angular-ui team might want to weigh in on this...

Also, in the second example, I'm not sure I understand the purpose of the redirect.

@Hyzhak, could this work for you, instead?

$stateProvider.state('venues', {
  url: '/location',
  resolve: {
    locationParams: function() {
      getPosition().then(function(value) {
        return value;
      });
    }
  },
  controller: function($scope, $state, locationParams) {
    // use locationParams here
  }
});

jmstout avatar Sep 03 '13 21:09 jmstout

@Hyzhak yeah I think you need to look into resolve some more. That may be the missing piece to your puzzle.

timkindberg avatar Sep 03 '13 21:09 timkindberg

You could even use onEnter instead of controller. That approach seems fine to me. $urlRouterProvider is intended for simple routing and redirection only.

nateabele avatar Sep 03 '13 22:09 nateabele

@jmstout it's nothing changes, except of calculation of locationParams into controllers we move it to resolvers. But in controller we still need to use

$state.go('venues', {zoom: value.zoom, lat: value.lat, lng: value.lng});

@nateabele but $urlRouterProvider already has function as second parameter and can contain any level of complexity, so it's not simple anymore.

@timkindberg hm., Can you describe more what you mean?

hyzhak avatar Sep 04 '13 06:09 hyzhak

@Hyzhak You don't need $state.go('venues', {...});

I don't think you're approaching the problem correctly.

The resolve property helps load the data you're retrieving prior to changing to the venues state. Visiting your defined url, '/location' should resolve the data and THEN transition to the state.

Make sure you've read this section of the wiki: https://github.com/angular-ui/ui-router/wiki#resolve

jmstout avatar Sep 04 '13 16:09 jmstout

@jmstout I think you have missed a point of my example.

I need to handle two url:

  • 1st with predefined position (location/zoom/lat/lng),
  • and 2nd with undefined position, that defines the position and redirect to predefined (/location/).

So there is the way to plume everything in one route with the resolver and optional parameters, but ui-route doesn't support them (your solution). The other way is to redirect 2nd route to 1st, route, but after we will get the position (my solution).

So is not an issue of resolvers it is issue for router and parameters. And yes I familiar with resolve.

But anyway it is one of the examples. Maybe not the most beautiful for such issue.

hyzhak avatar Sep 04 '13 16:09 hyzhak

I understand your use case, but this isn't a priority on the roadmap. However, you're welcome to submit a PR with a patch and a test case.

nateabele avatar Sep 10 '13 05:09 nateabele

I think I'm having the same issue as you guys: I have multiple nested states. Let's say:

departments:
/departments/:idDepartment

departments.group:
/group/:idGroup

departments.group.category:
/category/:idCategory

Each state has a resolve to retrieve his data, prerequisites. BTW, I use this workaround to use the parent states resolved data in the child states resolve (https://gist.github.com/MattWalker/6106393)

When the user goes to /processes I redirect him to /departments//group//category/ so he can see the default department with the default group and default category. I don't want him to select a department then a group then a category. I want to show him right away a view with default values.

It works but here is my question. Once the default values have been found and the view is shown, how can I have the url location display something like

/departments/1/group/23/category/2

instead of

/departments//group//category/

sveilleux avatar Oct 30 '13 17:10 sveilleux

Two options for you.

In release currently I believe you could do:

$urlRouterProvider.when('/location/', function($location) {
    getPosition().then(function(value) {
        $location.url('venues/' + value.zoom + '/' + value.lat + '/' + value.lng);
    });

    // it only cares that you are handling it, so tell it you are
    return true;
});

function getPosition() {
    var defer = $q.defer();
    defer.resolve('/location/1/2/3');
    return defer.promise;
}

Or in 0.3.0 you could use the deferIntercept feature: https://github.com/angular-ui/ui-router/blob/master/src/urlRouter.js#L200-L245

timkindberg avatar Jul 16 '14 02:07 timkindberg