dotJEM / angular-routing

Enhanced state based routing for Angular applications!
https://dotjem.github.io/angular-routing/
MIT License
75 stars 9 forks source link

Resolve section and $state.current #96

Closed vicentereig closed 10 years ago

vicentereig commented 10 years ago

Hi @jeme,

I was expecting to access the URL params in the resolve section. Basically when I hit the route /#/playlists/23 I would like to find a playlist by its id (23) as follows.

.state('playlists.show', {
  route: '/playlists/:id',
  views: {
    main: {
      controller: 'PlaylistController',
      template: 'application/templates/playlist/show.html'
    }
  },
  resolve: {
    playlist: ['PlaylistLoader', '$state', function(PlaylistLoader, $state){
      return PlaylistLoader.find($state.current.$params.id);
    }]
  }
});

At that point it happens that $state.current doesn't point to playlist.show but the one before in the current transition. —it could be either $root or i.e. playlist.index depending if the user entered first through the parent state—.

Question is: is this supposed to be the expected behaviour?

I'm coming from ui-router, where I could use the $stateParams from the resolve section. Is there a different way to access them during route loading?

I'm currently bypassing this issue using the following hack, basically using $location to extract the id from the path.

resolve: {
  playlist: ['PlaylistLoader', '$location', function(PlaylistLoader, $location){
    var playlistId = $location.$$path.split('/playlists/')[1];
    return PlaylistLoader.find(playlistId);
  }]
}
jeme commented 10 years ago

That is correct, current only changes until the full state transition is complete.

Instead you can use $to and $from respectively. E.g.:

angular.module('demo.about', ['dotjem.routing'])
  .config(['$stateProvider', function(sp){
    sp.state('about', {
      route: '/about',
      resolve: { something: ['$to',function(to) { 
          console.log(JSON.stringify(to));
        }] },
      views: { 
          main: {  template: 'about.html'
        } 
      }
    })
  }]);

http://plnkr.co/edit/hJl3mYs8ouUcBnGpyPhT?p=preview

vicentereig commented 10 years ago

Thanks, man. Good to know that there's finally a routing library out there working well. Let me update the docs, should I create a new dotjem.routing.$to.html under https://github.com/dotJEM/angular-routing/tree/master/doc/partials/api or is it an instance of $state and belongs to that page?

jeme commented 10 years ago

That would be very much appreciated.

API docs are generated using ng-docs. So the source for it is written in the source code it self.

I think it would be most appropriate if it was added as a type or interface to the docs... And then the documentation could fit nicely into this file: https://github.com/dotJEM/angular-routing/blob/master/src/state/state.ts As that is the closest place in the source code we get to a direct link to the state object passed into the state function, since it is merely a wrapper around it.

Unfortunately ng-docs isn't all that well documented it self AFAIK (I actually don't know of any documentation for it at all)... So most of it is going to the Angular Docs, find something that might fit into the concept, then see how they write their docs... E.g these sources:

Currently docs are not automatically build and published though, so it will be a while before it becomes available at: http://dotjem.github.io/angular-routing/#/docs/api/dotjem.routing

vicentereig commented 10 years ago

:+1: Never used ng-docs before, let me give it a shot!

jeme commented 10 years ago

If you have any questions feel free to ask.