I'm looking for a classy strategy to retrieve the current user information with emberjs (and ember-data).
Currently, I do the following :
My API responds to /users/me
with the current user.
In my adapter, when the retrieved object id isn't the one I'm hoping for, I add a real_id
which is the id (the user's id in the database) and I replace the real returned id by what I'm expecting.
So when my adapter has "me" as user's id, but the server returns 1, I get the following json :
{"real_id": 1, "id": "me"}
This works. But I'm not a big fan, and I'd like to avoid as mush as possible to change the default content of the adapter.
Do you use any strategy for this ? What would you recommend ?
I would use a controller App.currentUserController
for this.
App.CurrentUserController = Ember.ObjectController.extend({
content: null,
retrieveCurrentUser: function() {
var controller = this;
Ember.$.getJSON('/users/me', function(data) {
App.store.load(App.User, data);
var currentUser = App.store.find(data.id);
controller.set('content', currentUser);
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With