Where to store very global stuff like an Authentication Token or a scrope


#1

Hello

I’m very confused about the best practise to store very global stuff. I think there is a DI Framework, but its not well documentated, and really hard to understand.

So where should the globals that I need to fetch the models from the server store like an authentication token or a company name?

I Access my Models from Routes and Controllers, so I need to access the Globals from both, or directly from my Models.

I’ve parent routes for the globals, so I think about a way to access somehow the parent route from the controller and the nested routes and store it there, but how?

I can access parent models with modelFor on the route, but not on the controller, and controllerFor gives me the controller before the setupController hook runs so just a useless controller.

May I’m thinking in the wrong direction, but whats the right one?

thanks


#2

I simply like to stick a model to the application route that i can use for global application state like so:

App.ApplicationRoute = Ember.Route.extend
  model: ->
    Ember.Object.create(
      something: ...
    )

You can then access this with:

...
@.modelFor('application').get('something')

#3

Cool, just how to access from a controller? There is no modelFor on the controller.


#4

The easiest approach right now is to store it on the ApplicationController itself, which defaults to being a simple controller.

Then, you can grab it in routes via controllerFor and other controllers via needs.


Why is setupController called after the model hook of nested routes?
#5

We are thinking about a “service” approach that could be used for truly global things, and made accessible to routes and controllers (but not components or helpers, by default). If we make any progress on this line of thinking, we’ll make sure to gather community feedback.


#6

By globals do you mean “Configuration” type values or some type of Resources?

I am very interested in what the “Best Practice(s)” would be for defining and accessing what seem to me to be Static but Transient data. This could include translations resources (I use Ember i18n which works brilliantly for me) as well as Application State and Session State.

@wycats how would this apply to a “Session” state?


#7

@wycats Cool, Thanks, but when to set the data to the controller? The parent ‘setupController’ hook seems to be executed after the child route ‘model’ hook. So I access the controller before I’m able to set the data. How do handle this?


#8

@Brendan More like an authentication token. I’ve actually two values to store: The authentication token and something called wempf. The User is requested to select one direct after the login, and I need it then for all future server request. Its like a current working mode, with everything depends on, like rolebase visibility and so on.

Currently I store both, wempf and token directly on the application, and reset the application by reloading the page when the user wanna change it. It works, but is a bit hacky, so I’m looking for a better Solution.


#9

Discourse uses Singleton to store user session data.

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/mixins/singleton.js