Search code examples
javascripttitaniumappceleratorappcelerator-titanium

Override click event listener of a widget - Appcelerator Titanium


Is there any way that we can override the click event listener of an element of Widget in Titanium?

I have a widget and in a controller where I am using that widget, I need to perform some other action on click of the element than it is defined in the widget.

Is there any way to make it happen?

TIA :)


Solution

  • To override a given function i would export a setter function in the widget:

    Build the Widget as you would always do widget.xml:

    <Alloy>
        <Button onClick="doClick" title="Hello World!" />
    </Alloy>
    

    Export a setter function $.overrideListener to be able to override clickHandler from the outside of the widget widget.js:

    function doDefaultStuff(e){
        //Do default stuff
    }
    var clickHandler = doDefaultStuff
    function doClick(e){
        clickHandler(e)
    }
    $.overrideListener = function(callback){
        clickHandler = callback
    }
    

    index.xml:

    <Alloy>
        <Window>
           <Widget id="widget_id" src="com.widget.somewidget"></Widget>
        </Window>
    </Alloy>
    

    Override the clickHandler if you wish to do something different than the default behaviour index.js:

    function doCustomStuff(e){
        //Do Custom Stuff
    }
    $.widget_id.overrideListener(doCustomStuff)