Search code examples
javascriptdojodijit.form

Dojo: how to enhance a dijit?


is it possible to bind a certain behaviour to a dijit programmatically?

I.e. let’s say I’m using this dijit/form/NumberSpinner a lot all across my project. Now I want to have an onFocus: function() { console.log('hi') } on all NumberSpinners.

Usually I would do this:

          … new NumberSpinner({
                onFocus: function() { console.log('hi'); },
                …
            });

on every element. Isn’t there a way to bind this as default behaviour for every NumberSpinner instance??

Thanks


Solution

  • I finally found how to do this.

    Using Extend

    like this:

    lang.extend(NumberSpinner, {
        _onFocus: function() { console.log('hi'); }
    });
    

    Working example on JSBIN: https://jsbin.com/sesotolove/2/edit?html,output

    Reference: https://dojotoolkit.org/reference-guide/1.7/dojo/extend.html

    Using Prototype

    Same can be achieved by just overwriting the prototype like this:

    NumberSpinner.prototype._onFocus: function() { console.log('hi'); }