Dennis
Dennis

Reputation: 471

Emberjs only redirect to default route if no subroute is specified

I'm trying to redirect /dashboard/ to /dashboard/reach/demographic if the url hits /dashboard/.

Problem is, I still get redirected to /dashboard/reach/demographic/ if I hit a subroute like **/dashboard/traffic.

What is the Ember way of doing this?

Here is my code:

(function() {
    App.Router.map(function(match) {
      this.resource('dashboard', function() {
        this.resource('traffic', function() {
          this.route('visits');
          this.route('pageviews');
          return this.route('duration');
        });
        return this.resource('reach', function() {
          this.route('demographics');
          this.route('over_time');
          return this.route('devices');
        });
      });
      return this.route('audience');
    });

    App.DashboardRoute = Ember.Route.extend({
      redirect: function() {
        return this.transitionTo('reach.demographics');
      }
    });

    if (!App.ie()) {
      App.Router.reopen({
        location: 'history'
      });
    }

    App.reopen({
      rootUrl: '/'
    });

  }).call(this);

Upvotes: 5

Views: 775

Answers (1)

jaaksarv
jaaksarv

Reputation: 1480

Implement redirect in DashboardIndexRoute instead of DashboardRoute.

App.DashboardIndexRoute = Ember.Route.extend({
  redirect: function() {
    return this.transitionTo('reach.demographics');
  }
});

I created a jsbin example for you. Try:

http://jsbin.com/odekus/6#/dashboard/

http://jsbin.com/odekus/6#/dashboard/traffic

I also removed unnecessary return's from the code.

Upvotes: 7

Related Questions