Last Updated: February 25, 2016
·
753
· klclee

Useful Pattern for Emberjs to refresh client session

App.RefreshSession = (controller, cb) ->
   #do what you need here to refresh the session.. reading cookie etc

App.ServerProxy = (controller, cb) ->
   if App.CurrentSession?
      cb()
   else
     App.RefreshSession(controller,cb)

Call App.ServerProxy for all your server calls

Then in Ember return a promise to ensure things get synced especially if App.RefreshSession goes off to fetch stuff from the server so:

App =  Em.Application.create();
App.SomeRoute = Em.Route.extend({
    model: function(){
        return new RSVP.Promise(function(resolve, reject){ 
            App.ServerProxy(function(){....});
        });
    }
});