3

I want to use analytics tracking on every transition like mentioned in Mixpanel with EmberJS

In order to do so, I need to be able to reopen the Router.

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.

EDIT:

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

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)
Community
  • 1
  • 1
Josh Smith
  • 14,674
  • 18
  • 72
  • 118

1 Answers1

2

You can always get the session from Ember's container with

container.lookup('simple-auth-session:main');
marcoow
  • 4,062
  • 1
  • 14
  • 21
  • In the context of the `Router` how does one get the `container`? I've only ever seen that in the context of an initializer. – Josh Smith Jul 09 '14 at 19:18
  • Looks like probably in my case (in an `ember-cli` app) that would be `AppName.__container__.lookup('simple-auth-session:main')`. – Josh Smith Jul 09 '14 at 19:22
  • 1
    Actually, it's possible to do with `this.container`. – Josh Smith Jul 09 '14 at 19:35
  • 2
    Is this.container kosher? I thought it was for ember internals only (especially/specifically via App.__container__). Ok, I dug around and it sounds like container api is part public and in flux: http://stackoverflow.com/questions/14085749/what-is-the-purpose-of-the-ember-container – Amir T Jul 10 '14 at 17:16