Last active
January 31, 2016 17:55
-
-
Save dylanjha/11004125 to your computer and use it in GitHub Desktop.
Set a `currentUser` in Ember world that all controllers ( and all views ) can access. I'm using Ember 1.5.0 and Ember Data 1.0.0-beta.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>App</title> | |
<%= stylesheet_link_tag 'application', media: 'all' %> | |
<%= javascript_include_tag 'application' %> | |
<%= javascript_include_tag "templates/all" %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<%# Have Rails put the serialized User JSON into the DOM %> | |
<%= tag(:meta, content: UserSerializer.new(current_user).to_json(root: false), name: 'current-user') if user_signed_in? %> | |
<%= yield %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create an initailizer that does two things: | |
// 1. Parse the currentUser json from the meta tag that rails put in the DOM | |
// 2. Store that currentUser as a User Model into the data store - [ store.push() ] | |
// 3. Set the `content` property of CurrentUserController to the user instance | |
// 4. Inject currentUser into every controller | |
Ember.Application.initializer({ | |
name: "currentUser", | |
initialize: function(container, application) { | |
var store = container.lookup('store:main'), | |
attributes = $("meta[name='current-user']").attr('content'); | |
if(attributes){ | |
var object = store.push(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'); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.CurrentUserController = Ember.ObjectController.extend({ | |
isSignedIn: (function() { | |
return this.get('content') !== null; | |
}).property('content'), | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the User model, implementing `find`, used in the initializer | |
App.User = DS.Model.extend({ | |
email: attr('string'), | |
location: attr('string'), | |
profile_image_url: attr('string'), | |
name: attr('string') | |
}); | |
App.User.reopenClass({ | |
find: function(id){ | |
return this.store.find('user', id); | |
}, | |
findAll: function(){ | |
return this.store.findAll('user'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One nitpick: could you make
current_user_controller
usemodel
instead ofcontent
?