0

I'd like to display a "loading..." animation to the users logging into my application.

Ember automatically transitions to a 'loading' route (if any) if a model returns a promise, so it gives the opportunity to display a 'loading' template till the server responds.

Now, I have a login form where submit triggers the 'authenticate' action in my controller (which is defined in the LoginControllerMixin). This seems not to be recognised as a promise by ember, hence the application does not transition to the 'loading' route.

Maybe there is a way around this using simple-auth session state, but I can't figure it out

any help would be appreciated

balafi
  • 2,143
  • 3
  • 17
  • 20

2 Answers2

2

It's not that it isn't a promise, it's that it isn't part of a transition. If you want to modify the authentication mixin, you can have it manually transition to a loading route, then begin the promise, then transition to destination post authentication. Honestly, I'd be surprised if this is worth it, unless your back-end authenticates really slow.

You would change the authentication logic to something like this:

this.transitionTo('loading').then(function(){      
  authenticateLogicCall().then(function(){
    this.transitionTo('authenticatedResource');
  });
});
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
1

I think the loading routes only work nicely when there's a transition and the framework is waiting for the promise returned by the destination route's model hook to resolve. That's not the case with Ember Simple Auth's LoginControllerMixin's authenticate action though. To display a loading message you can simply override that authentication action though:

export default Ember.Controller.extend(LoginControllerMixin, { actions: { authenticate: function() { var _this = this; this.set('loading', true); this._super().then(function() { _this.set('loading', false); }, function() { _this.set('loading', false); }); } } });

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • I like this approach as I can display a loading message in the login form and not transition to a complete new screen. Login may fail and its nicer to display the failure message before leaving the form. I think that, since password is cleared, not showing any message gives a bit of uncertainty to the user even if the login takes only a second. Thanks! – balafi Jan 12 '15 at 09:17