【问题标题】:ember-simple-auth: Any way to get session from within the Router?ember-simple-auth:有什么方法可以从路由器中获取会话?
【发布时间】:2014-08-30 22:21:43
【问题描述】:

我想在 Mixpanel with EmberJS 中提到的每个转换中使用分析跟踪

为此,我需要能够重新打开Router

ember-simple-auth 有什么方法可以在那里获得当前会话吗?我的理解是它对所有路由和控制器都可用,但没有特别提到路由器。

编辑

我现在正在探索的另一种方法是在我想要进行分析识别的所有路由上包含一个 mixin。我有一个类似如下的mixin:

`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 似乎并没有单独包含user@store.find('user', userId) 也不起作用。

以下在模板中可以正常工作:

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.js ember-simple-auth


    【解决方案1】:

    你总是可以从 Ember 的容器中获取会话

    container.lookup('simple-auth-session:main');
    

    【讨论】:

    • Router 的上下文中,如何获得container?我只在初始化程序的上下文中看到过这种情况。
    • 在我的情况下(在ember-cli 应用程序中)看起来可能是AppName.__container__.lookup('simple-auth-session:main')
    • 其实this.container也是可以的。
    • this.container 洁净吗?我认为它仅适用于 ember 内部(特别是/特别是通过 App.__container__)。好的,我挖了一下,听起来容器 api 是部分公开的并且在不断变化:stackoverflow.com/questions/14085749/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多