Ember controller failing to update template upon property set within promise execution

I am facing an issue while integrating user login functionality in my application. After retrieving the user data from the server, I aim to display the user's name on the page once the process is completed. The login form appears as a popup window, inspired by this resource:

However, changing the template does not seem to work when I toggle the userLoggedIn variable to true. Could there be something wrong with my implementation?

App.UserController = Ember.Controller.extend({
    userLoggedIn: false,
    actions: {
        displayLoginForm: function () {
            // code to display login form goes here
        },
        recieveLogin: function (authResult) {
            // code to hide login form after successful authentication
            var userPromise = this.store.find('user', authResult); // successfully fetches user details from the server
            var self = this;
            userPromise.then(function (user) {
                self.set('model', user);
                self.set('userLoggedIn', true);
            });
        }
    }
});
<li class="navbar-form">
    {{#if userLoggedIn}}
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="#">My quizzes</a></li>
            <li><a href="#">My scores</a></li>
            <li><a href="#">Settings</a></li>
            <li class="divider"></li>
            <li><a href="#">Logout</a></li>
        </ul>
    {{else}}
        <button class="btn btn-default" {{action 'displayLoginForm'}}>Login</button>
    {{/if}}
</li>

Answer №1

The root cause of this bug was not what I initially believed it to be. The issue stemmed from the callback in the login script utilizing a [hacky] method to access the controller from a popup outside of the Ember framework:

window.opener.App.__container__.lookup('controller:User').send('recieveLogin', 'USERIDHERE');

Regrettably, this approach resulted in obtaining a different instance of the controller that did not execute the expected actions...

To resolve the issue, I made adjustments to the controller as follows:

App.UserController = Ember.Controller.extend({
    userLoggedIn: false,
    actions: {
        displayLoginForm: function () {
            //displays a login form
            /******** CRUCIAL LINE HERE ********/
            App.UserController.callback = this;
            /******** END OF CRUCIAL LINE ********/
        },
        recieveLogin: function (authResult) {
            //hides login form
            var userPromise = this.store.find('user', authResult); // successfully retrieves user data from server (to the best of my knowledge)
            var self = this;
            userPromise.then(function (user) {
                self.set('model', user);
                self.set('userLoggedIn', true);
            });
        }
    }
});

Subsequently, I employed a slightly less unconventional method on the page:

window.opener.App.UserController.callback.send('recieveLogin', 'USERIDHERE');

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

Challenges with Organizing Data and Maintaining Database Integrity

I have been working on making this sortable code function properly. Initially, I had it working fine with <li> elements as shown in the UI examples. However, now I am trying to implement it with <div> elements. While it shouldn't be much o ...

When submitting a form with the jQueryForm plugin, take action on the form by selecting it with `$(this)`

I have a situation where I have multiple forms on one page and am utilizing the jQuery Form plugin to manage them without having to reload the entire page. The issue arises when I need some sort of visual feedback to indicate whether the form submission wa ...

Changes to a key value are not reflected in the array of objects

When making changes to input fields within an array of records that include Date and Text fields in a table, the data is not updating as expected. I am encountering issues where changing the Date input results in undefined Text values, and vice versa. My g ...

Mongoose makes sure that duplicate rows are not repeated in the database

I'm working with a basic mongoose schema definition below: const mongoose = require('mongoose'); const followSchema = new mongoose.Schema({ follower: { type: mongoose.Schema.Types.ObjectId, ref: 'User', ...

Creating a Controlled accordion in Material-UI that mimics the functionality of a Basic accordion

I'm a Junior developer seeking assistance with my first question on this platform. Currently, I am working with a Controlled accordion in React and need the icon to change dynamically while staying open even after expanding another panel. The logic ...

The server node proxy is failing to trigger the API call

update 1: After modifying the api path, I am now able to initiate the api call. However, I encountered the following error: (node:13480) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): RangeError: Invalid status code: res ...

What is the process for integrating a tensorflow.js model into a React-based web application?

I've been working on a React web application in Typescript that involves loading a tensorflow.js model and then applying it each time the component updates. While I successfully implemented this in a small demo app without React, I am facing some chal ...

Error: The specified function in the schema is not valid for the current operation mode

I'm facing an issue with validating a material ui form using Formik and Yup. The error keeps popping up. This is the schema I imported from another file: export const validationSchema = Yup.object({ email: Yup.string() .email('Invalid Ema ...

Fade in and out a Div with a distinct class and ID

I'm currently experiencing a minor issue with some jQuery code. Below are some divs: <div class="add" id="1">Follow</div> <div class="added" id="1">Following</div> <div class="add" id="2">Follow</div> <div clas ...

The jQuery onClick function functions effectively for the initial two clicks; however, it ceases to

I am currently experimenting with jQuery to dynamically load a specific div from another page on my server into a designated section on the existing page. While the code is successfully functioning for the first two clicks on the website, it fails to work ...

Transforming JSON data into comma-delimited values (with thousands separators) using Angular 5 and ES6 syntax

Is there a more elegant method to convert a JSON response into comma-separated numbers for displaying currency purposes? Here is the code I have currently: let data = { "business":{ "trasactionTableData":[ { ...

Is there a way to stop jQuery dragging functionality from causing the page to scroll unnecessarily

Whenever I drag an element from div1 to div2, the scrollbar in div1 keeps extending until I drop the element in div2. How can I prevent this extension without breaking the element being dragged? <div class="container-fluid"> <div class="row"> ...

The AngularJS modal is sending back the results before updating the parent scope

When launching a modal from my web page, I am updating an array passed from the parent. However, when closing the modal and sending back the updated results, the parent scope object is also being updated. If the user decides not to update and cancels the ...

Unleash the power of a module by exposing it to the global Window object using the dynamic

In my development process, I am utilizing webpack to bundle and manage my TypeScript modules. However, I am facing a challenge where I need certain modules or chunks to be accessible externally. Can anyone guide me on how to achieve this? Additional conte ...

Ensure that ExpressJS response includes asynchronous try/catch handling for any errors thrown

Currently working with ExpressJS version 4.16.0, NodeJS version 10.15.0, along with KnexJS/Bookshelf, and encountering issues with error handling. While errors are successfully caught within my application, the problem arises when trying to retrieve the e ...

I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click. count = 1; function myF ...

Can a web application determine if Microsoft Excel has been installed?

Currently, I am developing a web application using ASP.NET that includes certain functionalities which rely on Microsoft Excel being installed on the user's device. In case Excel is not available, I would prefer to deactivate these features. I am foc ...

Developing a Customized Modal with Backbone.js

As a newcomer to Backbone, I have been trying to create a Modal in my application by setting up a base Modal class and then extending it for different templates. However, despite conducting some research, I haven't been able to find a solution that fi ...

Content update failed due to an error

Why is an error occurring when attempting to update the content? The edited value is retrieved in the componentDidMount function without any issues. Posting the content works fine, but updating it using reactjs and django tastypie api throws an error as fo ...

Similar to the filter() method in Vanilla Javascript but behaves in a equivalent way to

Looking to convert some JQuery code to Vanilla JS. $('#myID :not(a, span)').contents().filter( function() { return this.nodeType === 3 && this.data.trim().length > 0;}) .wrap('<span class="mySpanClass" />'); I&a ...