-
-
Save ivanvanderbyl/4560416 to your computer and use it in GitHub Desktop.
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'); | |
} | |
}); |
@ivanvanderbyl thanks for the comments. I think I like your suggestion to first look for a simpler way. I will try to reduce the number of roles in my app to 3 at the most and just go with something simple as you are doing. I can then revisit it in the future if need be.
This is awesome! Looking forward to using it.
@ivanvanderbyl did you ever figure out the issue with not having a content property set when you do a typeInjection with a controller? I believe I'm running in to the same issue.
Example...doing something like this:
Ember.Application.initializer
name: "currentUser"
after: 'session'
initialize: (container, application) ->
controller = container.lookup("controller:currentUser")
container.typeInjection('controller', 'currentUser', 'controller:currentUser')
Results in this chrome error:
Uncaught Error: assertion failed: Cannot delegate set('currentUser', <App.CurrentUserController:ember313>) to the 'content' property of object proxy <App.OtherController:ember848>: its 'content' is undefined.
Interestingly enough...its that OtherController
's content is undefined that's causing the error, I think. I've noticed if I simply add a blank currentUser
property to OtherController
then it works as well.
I assume there's not really a way to run typeInjection somewhere else and at a later time (then in an initializer)?
@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?
Looks like that issue is fixed in master
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')
@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
I couldn't get it to work, I kept returning Guest in my template view.
@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
@AlexanderZaytsev Thanks for that article, it helped a ton
Is there a non-private alternative to typeInjection
?
@amaanr @AlexanderZaytsev I use the readiness calls that @listrophy suggests.
Remember to tell currentUser Initializer to fire up after loading store, otherwise store will be undefined
Ember.Application.initializer({
name: 'currentUser',
after: 'store',
@AlexanderZaytsev you raise a good point, I might start using that pattern in the future. For now I'm passing the current user to a controller anyway, like
AccountController
for example, and interacting with the controller not the model.@boy-jer I'm not overly sold on either of those solutions. I think the real question here is figuring out what you need on the client side to control authorization to certain parts of your app, and then expose the properties to query it.
Admittedly I haven't needed to do any authorization stuff which was very complex, my app only has two roles, 'Normal user' and 'Account owner', which are distinguished by whether or not they own the current account, which is exposed much the same way as the current user, for sake of argument.
If you plan to implement a full blown authorization system client side, I would first look at simpler ways to solve the main business problem :)