Strategy for getting current user

I am looking for a stylish strategy for retrieving current user information using emberjs (and ember-data).

I am currently doing the following:

My API is responding /users/mewith the current user.
In my adapter, when the identifier of the returned object is not the one I hope for, I add real_idwhich is the identifier (the user identifier in the database), and I replace the real returned identifier with what I expect.

So, when my adapter has an “I” as the user id, but the server returns 1, I get the following json:

{"real_id": 1, "id": "me"}

It works. But I'm not a big fan, and I would like to avoid as little as possible in order to change the default contents of the adapter.

Are you using any strategy for this? What would you recommend?

+5
source share
2 answers

I would use a controller App.currentUserControllerfor 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);
        });
    }
});
+9
source
get = Em.get
set = Em.set
App.AccountController = Em.Controller.extend

        contentBinding: '_content.firstObject'

        init: ->
            set @, "_content", App.User.find()
-5
source

All Articles