ember-simple-auth:从路由器中获取会话的任何方式? [英] ember-simple-auth: Any way to get session from within the Router?
问题描述
我想在使用EmberJS的Mixpanel 中提到的每次转换都使用分析跟踪
I want to use analytics tracking on every transition like mentioned in Mixpanel with EmberJS
为了做到这一点,我需要重新打开 Router
。
In order to do so, I need to be able to reopen the Router
.
有没有办法使用 ember-simple-auth
来获取当前会话?我的理解是,它可用于所有路由和控制器,但没有特别提到路由器。
Is there any way with ember-simple-auth
to get the current session there? My understanding was that it's available to all the routes and controllers, but saw no mention of the Router specifically.
编辑:
我现在正在探索的另一种方法是在我想要进行分析识别的所有路线上添加一个mixin。我有一个mixin,如下所示:
An alternative approach I'm exploring right now is to include a mixin on all the routes where I want to do analytics identification. I have a mixin like the following:
`import Ember from 'ember'`
AnalyticsMixin = Ember.Mixin.create
beforeModel: (transition) ->
@_super(transition)
userId = @get('session.user_id')
if (!Ember.isEmpty(userId))
user = @store.find('user', userId)
username = user.get('username') # this doesn't work
我可以从会话对象获取 user_id
,尽管我做的 Session.reopen
似乎自己包含用户
。 @ store.find('user',userId)
工作。
I can get the user_id
from the session object, although the Session.reopen
that I did doesn't seem to include the user
on its own. Nor does @store.find('user', userId)
work.
以下内容在模板中正常工作:
The following works fine in a template:
Authentication =
name: "authentication"
before: "simple-auth"
initialize: (container) ->
Session.reopen
user: (->
userId = @get('user_id')
if (!Ember.isEmpty(userId))
return container.lookup('store:main').find('user', userId)
).property('userId')
container.register("authenticator:custom", CustomAuthenticator)
推荐答案
您可以随时从Ember的容器中获取会话
You can always get the session from Ember's container with
container.lookup('simple-auth-session:main');
这篇关于ember-simple-auth:从路由器中获取会话的任何方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!