The setupController function is not triggered when utilizing {{linkTo}} or transitioning to a specific route using transitionTo("path", model)

Have you ever encountered a situation where the setupController function does not get called when using {{linkTo}} in Ember.js? I have noticed this issue in my application, where linkTo works fine in one instance but fails to work in another. The only distinction between the two cases seems to be that in the first scenario, linkTo is used inside a loop, while in the second case it is not. Below is the relevant code snippet for the non-working scenario:

App.Router.map(function() {
    this.resource("search", { path: "/search/:args" });
});

App.SearchCriteria = Ember.Object.extend({ });

App.SearchRoute = Ember.Route.extend({
    serialize: function(model, params) {
        // .. some code that converts model to a string called args
        return {'args': args}

    },
    model: function(params) {
        // convert args, which is query string-formatted, to an object
        // and then make a App.SearchCriteria object out of it.
        return App.SearchCriteria.create($.deparam(params.args));
    },
    setupController: function(controller, model) {
        controller.set("searchCriteria", model);
    }
});

In the search template:

{{view Ember.Checkbox checkedBinding="searchCriteria.music"}} Music
{{#linkTo search searchCriteria}}Search{{/linkTo}}

The last thing I see in the logs is:

Transitioned into 'search' 

Usually, the setupController function should be called at some point during the transition, but for some reason, it is not happening. I also attempted using the {{action}} method to call a handler and then use transtionTo, but with no success.

UPDATE 1: More insights added

The key discrepancy between the working and non-working scenarios is that in the successful case, {{linkTo}} is invoked from the same template as that of the controller and router (i.e., the linkTo is in the search template and it's invoking the SearchRoute). In contrast, in the failing case, the {{linkTo}} is being called on the SearchRoute but from a different template associated with a distinct router).

Upon debugging Ember code in Chrome, it appears that the reason the setupController is not triggered is because partitioned.entered remains empty. In the functional case, it contains items.

  var aborted = false;
  eachHandler(partition.entered, function(handler, context) {
    if (aborted) { return; }
    if (handler.enter) { handler.enter(); }
    setContext(handler, context);
    if (handler.setup) {
      if (false === handler.setup(context)) {
        aborted = true;
      }
    }
  });

UPDATE 2: Root cause identified - possible bug?

Based on my analysis, it seems like the root cause of the handler not being triggered is due to the fact that the

partitionHandlers(oldHandlers, newHandlers)
method does not recognize any changes in the model, hence the handler is not fired.

To elaborate, consider the following part of the view:

{{view Ember.Checkbox checkedBinding="searchCriteria.music"}} Music
{{#linkTo search searchCriteria}}Search{{/linkTo}}

Even though the user modifies the checkbox state (altering the searchCriteria), Ember fails to acknowledge the change in searchCriteria, resulting in no action being taken.

Could this be a bug in Ember.js?

Answer №1

It's uncertain what issue you're facing, but this advice might be of assistance.

setupController gets invoked every time the route is accessed. However, the model hook may not necessarily run each time.

Refer to the Ember guide:

Note: When a route contains a dynamic segment, the model hook is only triggered when it is accessed via the URL. If the route is accessed through a transition (such as when using the link-to Handlebars helper), a model context is already supplied and the hook isn't executed. Routes without dynamic segments always execute the model hook.

In general, if you use the link generated by link-to to access the route, Ember won't activate the model hook for that route. Instead, it passes the model (specified in the link-to parameter) to that route.

The rationale behind this is that since the client already possesses the model context, Ember believes there is no necessity to retrieve it again from the server (which is the job of the model hook).

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

The mathematical logic behind navigating forward and backward in Angular

Calling all math experts! I need your help with a slider issue. The prevSlide function is working perfectly, but the nextSlide function seems to be starting off on the wrong foot. It goes 1-2-3 and then jumps to 2-3-2-3... It's definitely a math probl ...

The functionality of event.charCode is ineffective in Firefox

I'm in a dilemma with restricting my text field to only accept numbers. Currently, I have the following code snippet: onkeypress="return (event.charCode >= 48 && event.charCode <= 57)" The issue arises when I attempt to delete (using b ...

Best practice for updating Form.Control text using a custom onChange method

Today, I observed a unique behavior while utilizing a custom onChange in a Form.Control. The text in the field no longer updates when a file is selected. I thoroughly checked the Git documentation HERE, but unfortunately, it does not provide information on ...

Combining ng-filter and ng-repeat to iterate over key-value pairs in objects

Currently, I am utilizing the built-in Angular 1 filters in an attempt to filter out a property of an object. While Angular Filters typically only accept Arrays and not Objects, the structure of the web application prevents me from refactoring to pass an a ...

When attempting to execute a query in Node using oracledb, an error with the code 'npm ERR! errno 3221225477' occurred

Encountered the following error message in the command line: npm ERR! code ELIFECYCLE npm ERR! errno 3221225477 npm ERR! [email protected] start: `node ./bin/www` npm ERR! Exit status 3221225477 npm ERR! npm ERR! Failed at the [email protected] start sc ...

Numerous slideshows using identical scripts

I am facing an issue with my code for multiple slideshows. Currently, it works fine for a single slideshow but when I have multiple slideshows and mouse over any of them, all the slideshows start running due to sharing the same class. However, if I try to ...

A guide to JavaScript: Fetching and Parsing JSON Data from an API

Hey there! I've been working on using this code snippet in my defult.js file to call an API, but I'm having trouble figuring out how to read the output. It always seems to end up in the last else part. function fetchDataDist(APPID, flag, call ...

Struggling to display my array data retrieved from the database on my Angular 5 page

I hope everyone is doing well. I am currently facing a problem with retrieving data from Firebase. I have an array within an array and I want to display it in my view, but I am encountering difficulties. Let me share my code and explain what I am trying to ...

Empty array returned when using fetch in a for loop

Currently, I am developing a server route to execute API calls. I have encountered the need to make two separate fetch requests as I require additional information that is not available in the first fetch. The issue lies in declaring a variable outside o ...

Angular form encountering problem with receiving JSON data from Express route

Here is my angular controller for handling user sign-up/sign-in: angular.module('SignCtrl', []).controller('SignController', function($scope) { $scope.formData = {}; $scope.processForm = function() { $scope.submitted ...

Trigger the React custom hook when the form is submitted

I have developed a custom useFetch hook to handle API requests in various sections of the application. It works smoothly when a component is rendered, but I am facing an issue when trying to make a request upon form submission. An error message pops up say ...

Complete Guide to Node.JS CRUD Delete Functionality

Currently, I am developing a node.js CRUD application that interacts with MongoDB. The main features of this app are allowing users to upload photos, edit details related to the photos, and delete them from the database. However, I am facing challenges i ...

Troubleshooting problem with chat messages due to Firestore snapshot limit or limitToLast issues

I'm currently developing a chat application using React Native with Firestore integration. To retrieve the latest 25 messages in the Chat screen, I am ordering them by timeSent and limiting to last 25 items: firestore .collection('group') ...

The UI router experiences a crash and requires a refresh after precisely six clicks

Whenever the following sequence of events occurs, my application seems to malfunction. Upon clicking on a user and assigning them a role, the page refreshes. A separate GET request retrieves a list of all users currently in the class. After selecting ex ...

When using Next.js, importing multiple components from the index manifest will automatically import everything

CONTEXT: For a project, I have organized all my UI components in a /components directory. Currently, I export each component in its own file using the following format: export function Component1() { ... } and then import them individually into my page li ...

Guide to sending a HTTP POST request with parameters in typescript

I need assistance sending a POST request using parameters in the following format: http://127.0.0.1:9000/api?command={"command":"value","params":{"key":"value","key":"value","key":"value","key":value,}} I attempted to do this but encountered an issue: l ...

Vuejs - Display multiple dates within one component

I have developed various components (such as tables, selects, etc) that all rely on the same methods to access API information. The main objective is to be able to use these components across different pages of the application in a way that allows for fle ...

Transfer a file to Node server before sending it to S3 for storage

I need to convert an HTML webpage into a PDF, export it locally, and then save the file to my node server for uploading to S3. Any suggestions on how I can achieve this? Here is the pseudocode with the function for converting data to PDF: const convertDa ...

Issue with Facebook like button not consistently loading on the website

Getting ready to release my new iOS app that creates customized mp3s and distributes them through a CDN-hosted webpage. Check it out at . I've integrated the XFBML code from http://developers.facebook.com/docs/reference/plugins/like/ because it' ...

Enhancing nested mongoose arrays by a particular value for updates

In my mongoose schema, I have an array that contains items and another array. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var CompanySchema = new Schema({ dateCreated: { type: Date, default: Date.now }, ownerId: { typ ...