Ember.js: Get id from currently active route

落爺英雄遲暮 提交于 2019-12-10 19:55:47

问题


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();
    }
});

回答1:


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.



来源:https://stackoverflow.com/questions/15164703/ember-js-get-id-from-currently-active-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!