The inverse function for Ember Handlebars helper options is experiencing an undefined error

With a template in hand, I needed to toggle the display of certain text based on a method's return value. Research led me to the recommendation of using handlebars helpers for this purpose. So, I implemented a resetPassword helper within the controller. While the options.fn(this) part functions correctly, the options.inverse(this) does not, resulting in the common JS error

Uncaught TypeError: undefined is not a function
....

templates/reset-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:

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

Answer №1

Since JS and Ember aren't working as expected, a workaround has been implemented:

  {{#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}}

Below is the controller function:

 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()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please click the provided link to display the data in the div. Unfortunately, the link disappearance feature is currently not

What I'm Looking For I want the data to be displayed when a user clicks on a link. The link should disappear after it's clicked. Code Attempt <a href="javascript:void(0);" class="viewdetail more" style="color:#8989D3!important;">vi ...

What is the best way to utilize the ajax factory method in order to establish a $scoped variable?

One issue I frequently encounter in my controllers is a repetitive piece of code: // Get first product from list Product.get_details( id ) .success(function ( data ) { // Setup product details $scope.active_product = data; }); To avoid this ...

Overwriting Resolved Data in Angular UI-Router Child States

I'm facing an issue where the resolve function is the same in both parent and child states, but I need it to return different values based on the child state. Instead of customizing the implementation for each state, it seems to be inheriting the beha ...

Steps for incorporating code to calculate the total price and append it to the orderMessage

I am seeking help with this program that my professor assigned to me. The instructions marked by "//" are the ones I need to implement in the code, but I'm struggling to understand how to proceed. Any assistance would be greatly appreciated, even just ...

How can nextJS leverage async getInitialProps() method in combination with AWS S3?

I'm currently facing a challenge with executing an s3.getObject() function within an async getInitialProps() method in a nextJS project. I'm struggling to properly format the results so that they can be returned as an object, which is essential f ...

The addition of special characters to strings in TypeScript through JavaScript is not functioning as expected

I need assistance on conditionally appending a string based on values from captured DOM elements. When the value is empty, I want to include the special character "¬". However, when I try adding it, I get instead because the special character is not reco ...

Synchronize Protractor with an Angular application embedded within an iframe on a non-Angular web platform

I'm having trouble accessing elements using methods like by.binding(). The project structure looks like this: There is a non-angular website | --> Inside an iframe | --> There is an angular app Here's a part of the code I'm ...

React Native Material - Implementing a loading indicator upon button press

With React Native Material, I am trying to implement a loading feature when a button is clicked. The goal is to show the "loading" message only when the button is active, and hide it otherwise. Additionally, I would like for the loading message to disappea ...

Looking to transform a list into JSON format using JavaScript?

I have a collection that looks like this: <ol class="BasketballPlayers"> <li id="1">Player: LeBron, TotalPoints: 28753, MVP: true</li> <li id="2">Player: Steph, TotalPoints: 17670, MVP: true< ...

Achieving vertical center alignment in React Native: Tips and techniques

Just a quick heads-up: This question pertains to a school project. I'm currently knee-deep in a full-stack project that utilizes React Native for the front-end. I've hit a bit of a snag when it comes to page layout. Here's the snippet of my ...

Is there a way for me to adjust the typography background based on its current status?

Is there a way to dynamically adjust the background color of text based on the status value? Currently, when the status is pending, the background color defaults to yellow. For example, if the status changes to complete, I want the background color to ch ...

Executing a JavaScript/jQuery function on the following page

I'm currently working on developing an internal jobs management workflow and I'd like to enhance the user experience by triggering a JavaScript function when redirecting them to a new page after submitting a form. At the moment, I am adding the ...

What is the best way to iterate over each character in a string and trigger a function in JavaScript?

I am currently working on a project to create a random password generator. The code responsible for generating the password is functioning correctly. However, I am facing an issue with converting the characters of the password into phonetic equivalents. I ...

Transform JSON data into a Google Sheet using Google Apps Script

Having trouble inserting the JSON response into Google Sheet using Google Apps Script with the code below. Running into errors, even though I can't seem to pinpoint the issue. Take a look at the screenshot and code snippet provided: function myF ...

Guide on selecting every input field located within a table

My form is embedded within a table <form id="form"> <input type="submit" value="send" class="btn btn-w-m btn-primary" style="float: left;">Add transaction</input> <table class="table table-strip ...

Developing a Vue.js application with a universal variable

In the previous version of Vue.js, 0.12, passing a variable from the root component to its children was as simple as using inherit: true on any component that needed access to the parent's data. However, in Vue.js 1.0, the inherit: true feature was r ...

Having trouble with images not showing up on React applications built with webpack?

Hey there! I decided not to use the create react app command and instead built everything from scratch. Below is my webpack configuration: const path = require("path"); module.exports = { mode: "development", entry: "./index.js", output: { pa ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

The counterpart to Ruby's `.select{ |x| condition }` in Javascript/ React.js would be to

This javascript function in React.js utilizes a for loop to determine the opponent team: getOpponentTeam: function(playerTeamId){ var matches = this.state.matches; var player_team = this.state.player.team.name for (i in matches){ if (matches[i]. ...

Having trouble with sending an ajax PUT request

UPDATE: The issue of getting an undefined URI was resolved by storing $(this).attr('red') in a variable. However, the 500 Server error persists. UPDATE: For reference, the complete code can be found on GitHub. Just to ensure nothing was overlook ...