简体   繁体   English

Ember - 如何将属性从Routes文件传递到Handlebar模板并返回401

[英]Ember - How to pass a property from Routes file to a Handlebar template with 401 returned

The situation is I have a login screen which works except when there is a failed login (401 not authorized). 情况是我有一个登录屏幕,除非登录失败(401未授权)。 Basically it is very simple right now, the handlebar (template) is trying to access a property from the route to determine if the call to the back end (rails) failed. 基本上它现在非常简单,车把(模板)试图从路径访问属性以确定对后端(rails)的调用是否失败。

{{#if loginFailed}}
  <div class="alert">Invalid username or password.</div>
{{/if}}

The Route file looks something like this, I have omitted any sensitive code or working code that is not needed: Route文件看起来像这样,我省略了任何不需要的敏感代码或工作代码:

import Ember from 'ember';
import ajax from 'ic-ajax';

export default Ember.Route.extend({

loginFailed: false,
isProcessing: false,

beforeModel: function(){
    //some stuff 
},

actions: {
login: function() {
    this.setProperties({
      loginFailed: false,
      isProcessing: true
    });
    var _this = this;

    ajax({
        url: //something,
        type: 'post',
        crossDomain: true,
        data: //some stuff,
  }).then(
            function(result) {
                //logging in logic all works
            },
            function(error){
                if (error.jqXHR.status === 401)
                {

                    _this.set('isProcessing', false);
                    _this.set("loginFailed", true);
                }
            }
        );


  },
},

reset: function() {
  this.set('isProcessing', false);
  this.controller.set('password', '');
  this.controller.set('username', '');
}
});

I've done some debugging and it does indeed hit the error block for the ajax promise however it just seems to crap out. 我已经完成了一些调试,它确实击中了ajax承诺的错误块,但它似乎只是废话。 I believe it craps out because it's 401, limited resources have led me to this conclusion. 我相信这很糟糕,因为它是401,有限的资源使我得出这个结论。

You can see that loginFailed is the property I am trying to change and when 401 error happens, and on the template this is the property I am trying to access. 您可以看到loginFailed是我尝试更改的属性以及401错误发生时,并且在模板上这是我尝试访问的属性。

I am very new to Ember just working on it for little over a week now so any help would be amazing. 我对Ember很新,现在只用了一个多星期,所以任何帮助都会很棒。

Here are the versions I am using when running ember -v on my project: 以下是我在项目中运行ember -v时使用的版本:

version: 1.13.8
node: 0.12.7
npm: 2.13.4
os: win32 x64

You have to set property on controller not route. 您必须在控制器上设置属性而不是路由。

_this.set("controller.loginFailed", true);

Demo. 演示。

You cannot use a route's properties in the corresponding template. 您不能在相应的模板中使用路径的属性。 You can use controllers for this purpose or you can use model hook to pass all parameters. 您可以使用控制器来实现此目的,也可以使用模型挂钩来传递所有参数。

I think, a better alternative might be making your routes' templates as dummy as possible so that they just pass data and needed action handlers to the components. 我认为,更好的替代方案可能是使您的路由模板尽可能虚拟,以便它们只传递数据并将所需的操作处理程序传递给组件。

Most of our routes' templates contains only one component and all the logic lies on the components and their inner-components. 我们的大多数路由模板只包含一个组件,所有逻辑都位于组件及其内部组件上。 All the event handlers also lie in the components unless they need server calls. 除非需要服务器调用,否则所有事件处理程序也位于组件中。 So you should better move this code to your login component: 因此,您最好将此代码移动到您的登录组件:

{{#if loginFailed}}
  <div class="alert">Invalid username or password.</div>
{{/if}}

Than your route's template will be like this: 比你的路线的模板将是这样的:

{{my-login-component loginPressed="login" loginData=model}}

All the other logic will be in your component's js file. 所有其他逻辑都将在组件的js文件中。

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

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