0leg
0leg

Reputation: 14134

Dojo: how to emit 'change' properly on custom widget

So, imagine there is a listener for native dijit/form/Select element, which listens for change event:

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

Now, I have created my own custom widget similar to Select and trying to listen using the same code:

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

The onSelectClick looks like:

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

The problem is that in the second case onSelectClick doesn't receive any values (undefined).

Tried to add:

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

to widget's _setValueAttr, tried to remove it. No value has been passed.. checked how native Select is defined - https://github.com/dojo/dijit/blob/master/form/Select.js

Any suggestions?

Upvotes: 0

Views: 593

Answers (1)

ben
ben

Reputation: 3568

For the way you want to use it, you should do (in your custom widget):

1) inherits from dojo/Evented
2) add the following:

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

That should do the trick

However, this implementation has a drawback: If you change the value from another piece of code, the emit will still be send, and usually we want the emit ONLY for user events, not for programmatic changes.

Upvotes: 1

Related Questions