Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy to retrieve the current user

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 ?

like image 904
Damien MATHIEU Avatar asked Oct 08 '22 11:10

Damien MATHIEU


1 Answers

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);
        });
    }
});
like image 189
pangratz Avatar answered Oct 13 '22 10:10

pangratz