Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: transitionTo route, then to dynamic segment

Tags:

ember.js

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.

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

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts');
    }
});

What I'd like to do is to transition them to a specified :account_id route based on some criteria. At the moment I just want to get the first account in the array and use that. But in the future it could be a way to transition them to the last account they were viewing. Something like this:

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts/:account_id');
    }
});

The docs give "detail" but don't provide an example, only offering the following:

transitionTo (name, models)

Transition into another route. Optionally supply a model for the route in question. The model will be serialized into the URL using the serialize hook.

I've tried the following:

this.transitionTo('accounts/4');
Uncaught Error: assertion failed: The route accounts/4 was not found

this.transitionTo('accounts', Social.Account.find(1));
Uncaught More objects were passed than dynamic segments
like image 840
commadelimited Avatar asked Feb 26 '13 22:02

commadelimited


2 Answers

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

like image 169
Shimon Rachlenko Avatar answered Oct 24 '22 14:10

Shimon Rachlenko


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.

like image 44
mduvall Avatar answered Oct 24 '22 14:10

mduvall