Using Javascript/Ajax to manually delete an event handler from a Sys.EventHandlerList() can be achieved by following these

I have encountered a situation where I am working with two script controls, one containing the other. Successfully, I have managed to handle events from the child control on the parent using the following code snippet:

initialize: function() 
{
    this._autoComplete = $get(this._autoCompleteID);

    this._onAutoCompleteSelected = Function
      .createDelegate(this, this.handleAutoCompleteSelected);

    var autoControl = this._autoComplete.control;
    autoControl.addItemSelected(this._onAutoCompleteSelected);
    ...
}

The addItemSelected method on the child control is implemented as follows:

addItemSelected: function(handler) 
{

    list = this.getEvents();
    list.addHandler('userItemSelected', handler);

},

The getEvents method is responsible for initializing the event list as shown below:

getEvents: function() 
{

    if (this._events == null) 
    {
        this._events = new Sys.EventHandlerList();
    }

    return this._events;
},

The issue arises when trying to dispose of the parent control. The attempt is to mimic the initial process by running through the event handlers in the child control and removing them:

dispose: function() 
{
    var autoControl = this._autoComplete.control;
    autoControl.removeItemSelected(this._onAutoCompleteSelected);
    ...
}

However, it appears that the ".control" property no longer exists at disposal, possibly due to the fact that the child control has already been disposed. This led me to think about iterating through the event list on the child control and clearing all event handlers within it:

dispose: function() 
{
    list = this.getEvents();
    for(var item in list._list)
    {
        var handler;

        handler = list.getHandler(item);

        list.removeHandler(item, handler);
    }

    ....
}

Given this scenario, I am open to suggestions on a more optimal approach to handling this situation. Is there a better way to achieve the desired result?

Answer №1

Questioning the use of the "control" expando property on the DOM element as a reference to the control object, especially since it seems to be manipulated by the framework before disposal.

Have you experimented with replacing $get with $find and adjusting your references in this manner?:

initialize: function() 
{
    this._autoControl = $find(this._autoCompleteID);

    this._onAutoCompleteSelected = Function
      .createDelegate(this, this.handleAutoCompleteSelected);

    this._autoControl.addItemSelected(this._onAutoCompleteSelected);
}

dispose: function() 
{
    this._autoControl.removeItemSelected(this._onAutoCompleteSelected);
    this._autoControl = null;
}

Also, instead of accessing the DOM element directly through this._autoComplete, consider accessing it through the control object:

this._autoControl.get_element();

In essence, switch the approach from "get element => get control object" to "get control object => get element".

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

What steps can I take to identify and manage a browser's inability to play a media file?

I am currently utilizing a JavaScript utility to stream mp3 content. Unfortunately, there are instances where I direct users to a file that cannot be played due to external factors. In such cases, Firebug displays an error message indicating that the file ...

How to enable Autocomplete popper to expand beyond the menu boundaries in Material UI

Within my Menu component, I have an Autocomplete element. When the Autocomplete is clicked, the dropdown list/Popper appears but it's confined within the Menu parent. How can I make it so that the Autocomplete's dropdown list/Popper isn't re ...

Steps for refreshing a React component following a data fetch operation

Currently, I am in the process of learning how to use React for building web applications. I have a basic React app that demonstrates the following features: Fetching a list of users Displaying the names of the users on individual cards once they are fetc ...

Are there any available resources for comparing the performance of JavaScript libraries?

In preparing a presentation for my company, I am outlining the reasons for choosing jQuery as our primary JavaScript / AJAX library. While most of the work is already completed, it would be beneficial to include a comparison with other libraries, particul ...

Troubleshooting problems with sending data in Jquery Ajax POST Request

I've spent a considerable amount of time searching for a solution to why my post request isn't sending its data to the server. Oddly enough, I can successfully send the request without any data and receive results from the server, but when attemp ...

Ways to troubleshoot an issue that arises when the `onChange` event is not utilized in a radio button component on a

When using this component for radio buttons without the Onchange function, I sometimes encounter the following error on the page related to onUpdate: TypeError: this.props.onUpdate is not a function onChange(e) { let value = e.target.value; this ...

Exploring Laravel's method for retrieving data from multiple tables in the controller

In an effort to make my jQuery call more dynamic, I have a controller with the following method: public function api(Request $request , $project_id){ return Program::where('project_id',$project_id)->get(); } This results in: [{"id":178," ...

React: Remember to always retain the initial four characters when making changes

I have implemented an input component for phone numbers using the react native phone input library, which automatically adds the international code. However, I am facing an issue where the international code +234 is deleted when the user presses the back b ...

Is it possible to utilize Jsoup to extract specific portions of JavaScript code from a webpage?

I am looking to extract data from the following script: $(document).ready(function(){ $("#areaName").val(1);$("#state").val(29);$("#city").val(1); $("#subareaName").val(1);$("#lane").val(1); } Specifically, I need to retrieve values such as areaName ...

Issues encountered transferring data using wp_localize_script

I need help passing PHP data to a JavaScript script in my project. To achieve this, I am utilizing the wp_localize_script function. wp_register_script('googlechart', 'https://www.gstatic.com/charts/loader.js'); wp_register_script(&apos ...

Sending Argument from JSP to Javascript

How can I retrieve a variable in JavaScript from JSP? Here is the code snippet: <li class="cid<%=cat.getDisplayCategoryBO().getDispCtgrNo()%>"> <a href="<%=url%>" onclick=""><%=cat.getDisplayCategoryBO().getDispCtgrNm()%> ...

What's causing this MUI React data grid component to be rendered multiple times?

I have developed a wrapper for the MUI Data Grid Component as portrayed: Selection.tsx: import * as React from 'react'; import { DataGrid, faIR, GridSelectionModel } from '@mui/x-data-grid'; import type {} from '@mui/x-data-grid/t ...

JavaScript form button press tracker

Hello! I've been tackling a challenge involving a JavaScript function to count button clicks. The catch is, the button type is set to submit, causing the page to reload every time I click it. Below is the snippet of code that contains the problemati ...

iPhone height is not correct

One issue I faced was when trying to view a webpage in landscape mode on iPhones (specifically testing on model SE, but it appears that many people are experiencing the same problem with other iPhone models). I have created a simple example below, consist ...

You cannot modify a variable inside an event handler function unless it is declared globally in the code

I am facing an issue with my nodejs backend app that uses mongoose for database connections and queries within an event handler of socket.io. Specifically, the problem arises when I try to update a variable within a callback function of a mongoose query in ...

What is the proper method for overriding styles in material-ui v5 for properties that are not present in the themes components?

Currently, I am customizing MuiDataTables using the adaptv4theme in the following manner: declare module '@material-ui/core/styles/overrides' { export interface ComponentNameToClassKey { MUIDataTable: any; MUIDataTableFilterList: any; ...

What is the best way to pass a state within a route component in react-router?

... import { useNavigate, NavigateFunction } from "react-router"; ... function Form(): JSX.Element { const navigateToCountry = (country: string) => { // Code to navigate to country page with the given country } const [selectedCount ...

Is there a way to grab the inner content of an e-mail link by right-clicking on it?

I am currently developing a Chrome Extension that functions similarly to the "Search on Google" feature when you right-click on selected text. However, I am facing an issue with making it work when right-clicking on a mailto: email link. How can I extract ...

Having trouble loading SVG component in Next.js with SVGR integration

I recently obtained an SVG react component that was automatically converted from the SVGR playground page. I integrated it into my project and followed the installation instructions for @svgr/webpack and configured the setup as advised. However, upon loadi ...

Deleting a row from a table in AngularJS can be accomplished by following these steps

I am having trouble with deleting rows from a table using angularjs. When I try to delete a row, it ends up deleting the previous row instead of the correct one. How can I fix this issue? Please check out the working DEMO Here is the code snippet: < ...