Singleton model for registered user

I am processing user login / registration without ember, but I would like to process user profile in ember (edit profile). What would be the best course of action to have a singleton model that will handle the current user? I defined a simple user as:

App.User = DS.Model.extend({ email: DS.attr('string'), name: DS.attr('string'), }); 

I could fill it in App.ready , but I'm not quite sure how to do this with anything other than App.User.find(id) , and I don't know id. My server can only return the current user in this session, but how to use it in this case? Or am I handling this incorrectly?

thanks

+4
source share
2 answers

Discourse inserts the current user information into the base HTML page and preloads it into the User model in the application route:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/routes/application_route.js

Then the User model has enough information to access the / etc user profile:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/models/user.js

Session-related items (such as logging out) access a separate URL /session/ :

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse.js#L169

If you use sessions and want to avoid embedding current user information in your basic HTML page (if you use static content, etc.), you can also manually access the session-specific URL (e.g. /session/ ), which returns the currently registered user information in accordance with the current session.

+4
source

I basically do what you describe. I use the code below to call the backend to get a registered user and install it in the Ember app.

 var App = Ember.Application.create({ LOG_TRANSITIONS: true, }); App.deferReadiness(); $.ajax({ url: 'http://localhost:3000/login', dataType: 'json', success: function (resp) { console.log(resp.user); App.store.load(App.User, resp.user._id, resp.user); App.store.load(App.Company, resp.company._id, resp.company); var user = App.User.find(resp.user._id); var company = App.Company.find(resp.company._id); App.CurrentUserController.reopen({ content: user }); App.CurrentCompanyController.reopen({ content: company }); App.advanceReadiness(); }, error: function (resp) { console.log('failed: ' + resp); App.advanceReadiness(); } }); App.initialize(); 
+4
source

All Articles