Routing and permalinks

203 views
Skip to first unread message

Gwendall Esnault

unread,
Oct 11, 2013, 10:17:25 AM10/11/13
to ang...@googlegroups.com
I am working on a project that has to support permalinks. A given url with no particular pattern can then redirect to different controllers.

For example let's consider a platform with two objects: users and posts with their respective url patterns /users/:id and /posts/:id.
For now how I am doing it (with ui-router) is by defining a controller for permalinks that catches unmatched urls.

app.config(function($stateProvider) {
$stateProvider
.state('userState', {
                    url: '/users/:id'
   templateUrl: '/templates/users.html'
 , controller: 'UserController'
})
.state('postState', {
                    url: '/posts/:id'
    templateUrl: '/templates/posts.html'
  , controller: 'PostController'
})
.state('permalink', {
    url: '/:permalink'
 , controller: 'PermalinkController'
})
;
});

In the permalink controller I can then query my server to know what type of object this permalink corresponds to.

app.controller('PermalinkController', function($scope, $state, $stateParams, Permalink) {

Permalink.get({ permalink : $stateParams.permalink }, function (result) {
                       // returns an "object_type" that can be either "users" or "posts"
}, function (err) {
});

});

Now where I am blocked is to redirect the data to the appropriate controller without changing the url. Is it possible to invoke this other controller directly from the permalink controller? If so, how?

Hope I'm not missing anything obvious!
Message has been deleted

Gwendall Esnault

unread,
Oct 11, 2013, 1:26:54 PM10/11/13
to ang...@googlegroups.com
Alright, so for anyone bumping into the same situation, the obvious solution I implemented is calling the related state with the object ID as a parameter, like so.

Permalink.get({ permalink : $stateParams.permalink }, function (result) {
    // assuming result returns something like { object_type : 'users', 'object_id' : 123 }
    $state.transitionTo('userState', { id : result.object_id }, { location : false });
}, function (err) {
});

Works like a charm!

OpenNota

unread,
Oct 13, 2013, 3:06:02 AM10/13/13
to ang...@googlegroups.com
Interesting. Maybe I use it somewhere.
Reply all
Reply to author
Forward
0 new messages