Enabling Proper Emission of 'Change' in Custom Widgets at Dojo Practice

Picture this: there's a listener set up for the native dijit/form/Select element, ready to respond to the change event:

nativeSelectWidget.on('change', lang.hitch(this, onSelectClick));

Now, I've developed my own custom widget that closely resembles Select and attempted to listen using the same code:

mySelectWidget.on('change', lang.hitch(this, onSelectClick));

The function onSelectClick appears as follows:

_onTypeChange: function(value) {
  console.debug('The value is, value)
}

The issue here is that in the second case, onSelectClick isn't receiving any values (undefined).

I attempted to include:

on.emit(this.domNode, 'change', {
  bubbles: true,
  cancelable: true
});

in the widget's _setValueAttr, but it didn't work. No value was being passed through. I checked how the native Select is defined here: https://github.com/dojo/dijit/blob/master/form/Select.js

Any recommendations?

Answer №1

When creating your custom widget, ensure to follow these steps:

1) Inherit from dojo/Evented
2) Include the following code snippet:

_setValueAttr: function(value) {
    this._set('value', value);
    this.emit('change', value);
}

This approach should resolve the issue at hand.

Nevertheless, there is a limitation in this implementation: If the value is modified from another section of the code, the emit will still trigger. Typically, we prefer to emit events only for user interactions and not for programmatic changes.

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

Utilizing VueJs @error handler for managing broken image links

I am encountering a strange issue with the VueJS @error handler. My goal is to hide images with broken links and display a placeholder instead. However, when I have two images with broken links, only the first image displays the placeholder while the other ...

Postponed in JQUERY | AJAX

My requirement is to retrieve data using the "SetUnit()" function, which includes asynchronous AJAX services. I attempted to use Deferred as suggested in this link, but it did not work for my needs. Code...... Function function SetUnit(query) { var ...

There seems to be an issue with the integration of Bootstrap in my JavaScript file

Is there a way to ensure that each new data entry in the array appears next to each other rather than stacking beneath one another? This is a JavaScript file with HTML written in Bootstrap format. const collegeData = [{ name: "University of Penn ...

Select information from an array and store it within an object

I want to extract all objects from an array and combine them into a single object. Here is the current array data: userData = [ {"key":"firstName","checked":true}, {"key":"lastName","checked":true ...

a expanding list with automatically loaded information

After trying various lists found online without success, it seems that the issue may be due to the dynamic loading of the list content using JQuery ajax. During $(document).ready, the list is loaded and the HTML data is appended to the div. Subsequently, ...

Linking a variable in typescript to a translation service

I am attempting to bind a TypeScript variable to the translate service in a similar way as binding in HTML markup, which is functioning correctly. Here's what I have attempted so far: ngOnInit() { this.customTranslateService.get("mainLayout.user ...

What is the best way to retrieve the value of an UL LI element using jQuery

I'm struggling to retrieve the value of a ul li listbox in a function, and I can't seem to figure out how. Here is my code: <ul class="dropdown-menu" id="newloc"> <?php $res_add = DB::table('res_br')-& ...

What is the most effective way to compare a property with the values stored in an array of objects?

d : {"children":[{"name":"China","children":[{"name":"China","value":400,"percentage":"33.33"}],"index":0},{"name":"England","children":[{"name":"England","value":300,"percentage":"33.33"}],"index":1},{"name":"Malaysia","children":[{"name":"Malaysia","val ...

Is it possible to categorize elements for focus and onblur events?

In my search feature, I have implemented an autocomplete div that appears when the user types in a search field. The issue I am facing is that I want the div to disappear when the user clicks outside of it. Here is what I have attempted: //SHOW THE DIV WH ...

Is there a way to use Lodash to quickly return the same value if a condition is met using the ternary operator

Is there a condensed way to do this using Lodash? (or perhaps Vanilla JS/TypeScript) var val = _.get(obj, 'value', ''); Please note that var val = obj.value || ''; is not suitable as it considers 0 and false as valid but fal ...

How do I adjust brightness and contrast filters on a base64 URL?

When presented with an image in base64 format like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDAAwfqNRk/rjcYX+PxrV/wtWqwJSTlboMgAAAABJRU5ErkJggg== What is the most efficient method to programmatically alter a filter (such as brightness or cont ...

The redirect function is failing to carry the "req" parameter

Express Routes Troubleshooting app.get('/auth/google/redirect', passport.authenticate('google'), (req, res) => { console.log('req.user:', req.user) //>>>>>Outputs {username: 'bob', id: '.. ...

Creating a form in Vue that dynamically generates checkbox options

In my Vue application, I have a dynamically generated form with v-models within a loop. Everything is working correctly except for checkboxes. The issue I am facing is that the v-model is affecting all the loops instead of just one, unlike with the input t ...

Steering clear of Unique error E11000 through efficient handling with Promise.all

In my development work, I have been utilizing a mongoose plugin for the common task of performing a findOrCreate operation. However, I recently discovered that running multiple asynchronous findOrCreate operations can easily result in an E11000 duplicate k ...

Creating a custom synchronization method in backbone.js to implement a loading animation

I am currently developing an app using the backbone.js framework and I want to implement a global loading effect whenever there is an HTTP request being made to the server. Since this is a single page webapp with multiple asynchronous requests, I believe m ...

What am I doing wrong that causes me to repeatedly run into errors when trying to build my react app?

While attempting to set up a react.js web application in VScode using "npm," I encountered the following errors: npm ERR! code ERR_SOCKET_TIMEOUT npm ERR! errno ERR_SOCKET_TIMEOUT npm ERR! network Invalid response body while trying to fetch https://registr ...

Adding a basic fade-in effect to an element that is lazily loaded using JavaScript

Is there a way to enhance my lazy loading script with an animation effect, such as 'show(slow)', so that each post fades in as it loads? This is my current JavaScript: jQuery(function(){ var page = 2; var myurl = blogUrll var lo ...

Locate the highest and lowest values within a .json file

When working on a graph using d3.js, one key step is setting up the scales for the axis. The data for the graph is stored in a Json file with multiple arrays, each representing different regions and years: [{ "id" : "AustraliaNewZealand" , "year" ...

Populating the array by calculating the average values

I'm encountering an issue in my JavaScript code. I have a situation where I must fill gaps in an array with the averages of the surrounding values. Let me provide an example: Array: 1, 2, 3, ,4, 5 In this case, I would need to fill the gap with the ...

Is it possible to use Harvest's Chosen alongside the Python Pyramid Framework?

Is it possible to set up npm to install chosen on a Linux server running Fedora in Amazon's AWS service? If not, are there any alternatives available? I'm currently using a python framework and wondering if it's safe to install node.js on t ...