简体   繁体   English

在ember-simple-auth身份验证后触发向“加载”路由的转换

[英]Trigger transition to 'loading' route after ember-simple-auth authentication

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. 如果模型返回了承诺,Ember会自动过渡到“加载”路线(如果有),因此它有机会显示“加载”模板,直到服务器响应为止。

Now, I have a login form where submit triggers the 'authenticate' action in my controller (which is defined in the LoginControllerMixin). 现在,我有了一个登录表单,其中Submit触发了我的控制器中的“身份验证”操作(在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 任何帮助,将不胜感激

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. 如果要修改身份验证混合,则可以手动将其转换为加载路由,然后开始promise,然后转换为目标后身份验证。 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');
  });
});

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. 我认为加载路由仅在发生过渡且框架正在等待目标路由的model钩返回的promise解析时才能很好地工作。 That's not the case with Ember Simple Auth's LoginControllerMixin's authenticate action though. 但是,Ember Simple Auth的LoginControllerMixin的authenticate操作不是这种情况。 To display a loading message you can simply override that authentication action though: 要显示加载消息,您可以通过以下方法简单地覆盖该authentication操作:

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); }); } } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM