Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Last active January 31, 2016 17:55
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ivanvanderbyl/4560416 to your computer and use it in GitHub Desktop.
Save ivanvanderbyl/4560416 to your computer and use it in GitHub Desktop.
Using Ember initializers and injections to setup the `currentUser` within your app.
App.AccountEditRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('content', this.get('currentUser'));
}
});
%noscript
.container
.alert
%strong
Javascript is disabled!
The AppName UI is built entirely in Javascript, as such you need to enable
Javascript in your browser to continue.
:javascript
var currentUser = jQuery.parseJSON('#{current_user_json}');
Ember.Application.initializer({
name: "currentUser",
initialize: function(container, application) {
var store = container.lookup('store:main');
var obj = store.load(CrashLog.User, currentUser);
container.optionsForType('user', { instantiate: false, singleton: true });
container.register('user', 'current', CrashLog.User.find(obj.id));
}
});
Ember.Application.initializer({
name: "injectCurrentUser",
after: 'currentUser',
initialize: function(container) {
container.injection('controller:application', 'currentUser', 'user:current');
container.typeInjection('route', 'currentUser', 'user:current');
}
});
@brennanmceachran
Copy link

@conrad-vanl & @ivanvanderbyl Would love to know if you guys got around the content is undefined error?

@rlivsey solves it in his pusher example by reopening the ControllerMixing and setting the value to null...

Ember.ControllerMixin.reopen({
  pusher: null
});

See: http://livsey.org/blog/2013/02/10/integrating-pusher-with-ember/

How did you guys get around this?

@brennanmceachran
Copy link

Looks like that issue is fixed in master

@rromanchuk
Copy link

FYI @AlexanderZaytsev I couldn't get this working as the $('meta[name="current-user"]').attr('content') dom element was not yet loaded. I finally got it working by adding a ready block

Ember.Application.initializer

  name: 'currentUser'

  initialize: (container) ->
    $ ->
      store = container.lookup('store:main')
      attributes = $('meta[name="current-user"]').attr('content')
      console.log attributes
      if attributes
        object = store.load(App.User, JSON.parse(attributes))
        user = App.User.find(object.id)

        controller = container.lookup('controller:currentUser').set('content', user)

        container.typeInjection('controller', 'currentUser', 'controller:currentUser')

@kristianmandrup
Copy link

@boy-yer your Approach #2 looks interesting, but where is the current_permission method you are delegating the can_update etc. to in the ApplicationController? and why do you pass an array with these values to the serializer? Something I'm not getting here... please advice. Thanks!

I'm trying to collect various Auth solutions in a new gem ember-beercan https://github.com/kristianmandrup/ember-beercan

@amaanr
Copy link

amaanr commented May 14, 2013

I couldn't get it to work, I kept returning Guest in my template view.

http://stackoverflow.com/q/16548010/1515899

@listrophy
Copy link

@amaanr @AlexanderZaytsev Perhaps hooking into the deferReadiness and advanceReadiness methods is necessary?

Ember.Application.initializer

  name: 'currentUser'

  initialize: (container) ->
    App.deferReadiness()
    $ ->
      store = container.lookup('store:main')
      attributes = $('meta[name="current-user"]').attr('content')
      console.log attributes
      if attributes
        object = store.load(App.User, JSON.parse(attributes))
        user = App.User.find(object.id)

        controller = container.lookup('controller:currentUser').set('content', user)

        container.typeInjection('controller', 'currentUser', 'controller:currentUser')
      App.advanceReadiness();

This might not work at all... I just learned about the Readiness methods a few minutes ago. :P

@DougPuchalski
Copy link

@AlexanderZaytsev Thanks for that article, it helped a ton

@DougPuchalski
Copy link

Is there a non-private alternative to typeInjection?

@DougPuchalski
Copy link

@amaanr @AlexanderZaytsev I use the readiness calls that @listrophy suggests.

@tomash
Copy link

tomash commented Jun 3, 2014

Remember to tell currentUser Initializer to fire up after loading store, otherwise store will be undefined

Ember.Application.initializer({
  name: 'currentUser',
  after: 'store',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment