Search code examples
ember.jsember-dataember-router

Ember.js: Get id from currently active route


Probably the most straight-forward question I've asked recently.

Given a route set up like so:

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

And a URL like so:

/accounts/12

How would I get the account_id if I wanted to get the currently active account record? The end goal is that I'm doing this:

acct = App.Account.find(12)

When I'd rather do this

acct = App.Account.find(account_id)

UPDATE 1

Here's the AccountsRoute:

Social.AccountsRoute = Ember.Route.extend({
    model: function() {
        return Social.Account.find();
    }
});

Solution

  • There is no canonical mechanism in Ember to store and retrieve the current user. The simplest solution is probably to save your current user's account ID to Social.currentAccountId when the user is authenticated (ie. in the success callback of your ajax request). Then you can perform

    Social.Account.find(Social.get('currentAccountId'))
    

    when you need to get the current user.