简体   繁体   English

Ember Handlebars helper options.inverse undefined不是一个函数

[英]Ember Handlebars helper options.inverse undefined is not a function

So I have a template and I need to show/hide some text based on a return value from a method. 所以我有一个模板,我需要根据方法的返回值显示/隐藏一些文本。 I searched and noticed one should use handlebars helpers in order to achieve this. 我搜索并注意到应该使用车把助手来实现这一目标。 So I added a resetPassword helper inside the controller. 所以我在控制器中添加了一个resetPassword帮助器。 The options.fn(this) part works. options.fn(this)部分有效。 The options.inverse(this) doesn't. options.inverse(this)没有。 It throws the ubiquitous JS error Uncaught TypeError: undefined is not a function .... 抛出无处不在的JS错误Uncaught TypeError: undefined is not a function ....

templates/reset-password.hbs: 模板/复位password.hbs:

<div class = "container">
  {{#resetPassword}}
      <h4>Password has been reset</h4>
      <h5>Your new password is: <b>{{password}}</b></h5>
  {{else}}
      <h4>Something went wrong! </h4>
      <h5>The password has not been reset! Please try again later.</h5>
  {{/resetPassword}}
</div>

controllers/reset-password.js: 控制器/复位password.js:

export default Ember.Controller.extend({

  token:       null,

  init: function ()
  {
    this._super();
    Ember.Handlebars.registerHelper('resetPassword', function (options)
    {
      var token = this.get('token');
      var result = false;
     /* Ember.$.ajax({
        type:        "POST",
        url:         "/reset_password",
        contentType: "text/html",
        dataType:    "json",
        async:       false,

        beforeSend: function (request)
        {
          request.setRequestHeader("Authorization", token);
        },

        success: function (data, textStatus)
        {
          this.set('password', data.password);
          result = true;
        },

        error: function (data, textStatus)
        {
          result = false;
        }
      });*/
      if (result)
      {
        return options.fn(this);
      }
      return options.inverse(this);
    });
  }
});

So because JS and Ember purely suck, here's a workaround: 所以因为JS和Ember纯粹是吮吸,这是一个解决方法:

  {{#if resetPassword}}
      <h4>Password has been reset</h4>
      <h5>Your new password is: <b>{{password}}</b></h5>
  {{else}}
      <h4>Something went wrong! </h4>
      <h5>The password has not been reset! Please try again later.</h5>
  {{/if}}

And the controller action: 和控制器动作:

 resetPassword: function ()
                   {
                     var self = this;
                     var token = this.get('token');
                     var result = false;
                     Ember.$.ajax({
                       type:        "POST",
                       url:         "/api/users/reset_password",
                       contentType: "text/html",
                       dataType:    "json",
                       async:       false,

                       beforeSend: function (request)
                       {
                         request.setRequestHeader("Authorization", token);
                       },

                       success: function (data, textStatus)
                       {
                         var responseUser = data["users"][0];
                         self.set('password', responseUser.password);
                         result = true;
                       },

                       error: function (data, textStatus)
                       {
                         result = false;
                       }
                     });
                     return result;
                   }.property()

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

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