Ember.js: transitionTo route, then to dynamic segment

前端 未结 5 1219
北荒
北荒 2021-02-01 07:45

I have a Router set up with accounts, and account/:account_id options. When the user lands on the index page of my app I transition them to the accounts route.

S         


        
相关标签:
5条回答
  • 2021-02-01 08:05

    As your /:account_id is a resource you need to transitionToRoute 'account'. You also need an AccountRoute accordingly.

    If /:account_id was a route not a resource you would transitionToRoute 'accounts.account' and your route handler would be called AccountsAccountRoute.

    0 讨论(0)
  • 2021-02-01 08:12

    I put together others' answers and some fiddling and came out with this answer:

    define your routes like:

    this.resource('accounts', function () {
        this.route('account', {path: '/:account_id'});
    });
    

    redirect:

    this.transitionTo('accounts.account', accountObj);
    

    But if you are loading from server, you need the accountObj object loaded before redirect:

    var accountObj = App.Account.find(1);
    accountObj.one('didLoad', this, function () {
        this.transitionTo('accounts.account', accountObj);
    });
    

    I set up this fiddle with complete example

    0 讨论(0)
  • 2021-02-01 08:18

    You are not specifying the route path properly and you should have a route under the resource, not another resource. It should be like this:

    Social.Router.map(function() {
        this.resource('accounts', function(){
            this.route('account', { path: '/:account_id'});
        });
    });
    
    Social.IndexRoute = Ember.Route.extend({
        redirect: function() {
            this.transitionTo('accounts.account', Social.Account.find(1));
        }
    });
    
    0 讨论(0)
  • 2021-02-01 08:24

    It looks like transitionTo is deprecated in favor of transitionToRoute.

    Nonetheless you can achieve the reroute by having the original declaration be this.resource('account', { path: '/:account_id'}); and then the transition with the single created object should work.

    0 讨论(0)
  • 2021-02-01 08:29

    With latest Ember (1.0.0-RC-6), this works perfectly.

    Router:

    this.resource('accounts', function () {
        this.resource('account', {path: '/:account_id'});
    });
    

    To redirect:

    this.transitionToRoute('account', Social.Account.find(1))
    
    0 讨论(0)
提交回复
热议问题