Last active
August 26, 2015 20:50
-
-
Save mattboldt/a42fd48ea5e4996b4bc3 to your computer and use it in GitHub Desktop.
Ember 2.0 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
`import Ember from 'ember';` | |
`import Resolver from 'ember/resolver';` | |
`import loadInitializers from 'ember/load-initializers';` | |
`import config from './config/environment';` | |
`import TruncateTextHelper from 'dochub/helpers/truncate-text';` | |
`import DS from 'ember-data'` | |
App = Ember.Application.extend( | |
modulePrefix: config.modulePrefix | |
podModulePrefix: config.podModulePrefix | |
Resolver: Resolver | |
) | |
loadInitializers App, config.modulePrefix | |
DS.Model.reopen | |
currentUserService: Em.inject.service('current-user') | |
`export default App;` |
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
`import Ember from 'ember';` | |
`import CurrentUserObjectProxy from "../models/current-user-object-proxy"` | |
CurrentUserInitializer = | |
name: "currentUser" | |
after: "store" | |
initialize: (container, app) -> | |
app.inject 'route', 'currentUserService', 'service:current-user' | |
app.deferReadiness() | |
store = container.lookup("store:main") | |
proxy = CurrentUserObjectProxy.extend() | |
container.register "user:current", proxy, | |
singleton: true | |
proxy = container.lookup("user:current") | |
$.ajax('/api/users/current-user').then (userPayload) -> | |
if userPayload | |
store.pushPayload('user', userPayload) | |
user = store.find('user', userPayload.user.id) | |
proxy.set("content", user) | |
app.inject("controller", "currentUser", "user:current") | |
app.inject("route", "currentUser", "user:current") | |
app.inject("component", "currentUser", "user:current") | |
app.inject("service", "currentUser", "user:current") | |
user.then (curUser) -> | |
$.ajax('/api/users/has_session_token').then (hasSessionRes) -> | |
curUser.set('hasSessionToken', hasSessionRes.has_session_token) | |
app.advanceReadiness() | |
else | |
app.advanceReadiness() | |
, (err) -> | |
app.advanceReadiness() | |
`export default CurrentUserInitializer` | |
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
`import DS from 'ember-data'` | |
CurrentUserObjectProxy = Ember.ObjectProxy.extend | |
isSignedIn: Em.computed.bool 'content.isLoaded' | |
`export default CurrentUserObjectProxy` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment