blueFast
blueFast

Reputation: 44371

Selectively trigger redirect

This is a part of my router:

this.resource('nodes', function () {

    this.resource('allNodes', function () {
        this.route('index');
        this.route('new');
        this.route('show',   { path: '/:node_id/show' });
        this.route('edit',   { path: '/:node_id/edit' });
        this.route('delete', { path: '/:node_id/delete' });
    });

    this.resource('services', function () {
        this.route('index');
        this.route('new');
        this.route('show',   { path: '/:service_id/show' });
        this.route('edit',   { path: '/:service_id/edit' });
        this.route('delete', { path: '/:service_id/delete' });
    });
...

In the /nodes route I do a redirect to allNodes.index:

SettingsApp.NodesRoute = Ember.Route.extend({
    redirect: function () {
        console.log('NodesRoute.redirect > redirecting to %s', ROUTES.ALL_NODES);
        this.transitionTo(ROUTES.ALL_NODES);
    }
});

Now in some other part of my application I am linking to a service directly with the url:

#/nodes/services/71ae38cde8584dc9c4eea25e74e68310/show

But this is not working, since the redirect of /nodes is kicking in. How can I prevent this?

The redirect must be active only when going to /nodes, not any sub-url. Must I implement this filtering by hand by checking the currentPath before redirecting, or is there a standard way of doing this?

Upvotes: 0

Views: 78

Answers (2)

selvagsz
selvagsz

Reputation: 3872

Do your redirection in NodesIndexRoute.

 App.NodesIndexRoute = Ember.Route.extend({
   beforeModel: function () {
    this.transitionTo('allnodes');
   }
 });

Working Model

Upvotes: 1

Kingpin2k
Kingpin2k

Reputation: 47367

You'll need to implement it by hand (the information is provided in the hook though)

Addiitionally redirect is a deprecated hook, in favor of the afterModel hook.

SettingsApp.NodesRoute = Em.Route.extend({
  afterModel: function(resolvedModel, transition){
     if(transition.targetName == 'nodes'){
        this.transitionTo(ROUTES.ALL_NODES);
     }
  }
});

Upvotes: 1

Related Questions