Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
});
like image 770
commadelimited Avatar asked Jan 27 '26 10:01

commadelimited


1 Answers

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.

like image 120
ahmacleod Avatar answered Jan 30 '26 07:01

ahmacleod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!