Created
August 29, 2014 02:24
-
-
Save artburkart/e859c53147a4b9ef8614 to your computer and use it in GitHub Desktop.
deferReadiness() to load current user
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
/* | |
* This was the quickest way I could figure out how to load | |
* the authenticated user into the app. It would make more sense | |
* if I only made one query to the server for the authenticated user | |
* (I could put the user id into a hidden input and load it from there) | |
*/ | |
App.initializer({ | |
name: 'loadCurrentUser', | |
after: 'store', | |
initialize: function (container, app) { | |
// Grab the store | |
var store = container.lookup('store:main'); | |
// Grab the controller to which the authenticated user will be added | |
var ctrl = container.lookup('controller:application'); | |
// Defer the app's readiness, so authenticated user can be fetched | |
app.deferReadiness(); | |
// Retrieve the id of authenticated user from '/session' | |
$.getJSON('/session').then(function (data) { | |
if (data && data.session && data.session.id) { | |
// Find the associated user | |
store.find('owner', data.session.id).then(function (user) { | |
// Set its value on the controller | |
ctrl.set('currentUser', user); | |
// Allow the app to continue | |
app.advanceReadiness(); | |
}); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment