blueFast
blueFast

Reputation: 44381

Redirect only when the parent is directly addressed

This is my code:

App.Router.map(function () {

    ....

    this.resource('profile', function () {
        this.route('user');
        this.route('user.edit', { path: '/:user_id/edit' });
        this.route('password');
        this.route('company');
        this.route('company.edit', { path: '/:company_id/edit' });
        this.resource('products', function () {
            this.route('index');
            this.route('show',   { path: '/:product_id/show' });
        });
    });

    ....

});

App.ProfileRoute = Ember.Route.extend({
    redirect: function () {
        this.transitionTo('profile.user');
    }
});

And I have a link-to:

{{#link-to "products.index" }}<i class="icon-barcode icon-2x pull-left icon-border"></i>{{/link-to}}

Which is linking to #/profile/products/index. But this triggers the redirect in ProfileRoute, which is not desired: the redirect should only be triggered whenever #/profile or #/profile/index is accessed.

How can I do this?

Upvotes: 1

Views: 70

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

Change from App.ProfileRoute to App.ProfileIndexRoute. Like the following:

App.ProfileIndexRoute = Ember.Route.extend({
    redirect: function () {
        this.transitionTo('profile.user');
    }
});

App.ProductsRoute have as parent the App.ProfileRoute route, so the redirect (a.k.a beforeModel), is triggered, and you get your undesired behavior. Using App.ProfileIndexRoute fixes it, because the App.ProfileIndexRoute is just triggered when calling /profile

I hope it helps

Upvotes: 1

Related Questions