royale-asjs icon indicating copy to clipboard operation
royale-asjs copied to clipboard

Problem using element.addEventListener

Open raudjcholo opened this issue 5 years ago • 2 comments

When I add an event listener to an element, for example this way textInput.element.addEventListener('focus', onInputFocus), listeners previously assigned to the object (textInput in the example) are removed.

raudjcholo avatar Dec 11 '20 15:12 raudjcholo

Trying this code seems to work ok:

Component Tester

package component
{
    import org.apache.royale.core.StyledUIBase;
    
    COMPILE::JS
    {
    import org.apache.royale.core.WrappedHTMLElement;
    import org.apache.royale.html.util.addElementToWrapper;
    import org.apache.royale.events.MouseEvent;
    }

    public class Tester extends StyledUIBase
    {
        public function Tester()
        {
        }

        COMPILE::JS
        override protected function createElement():WrappedHTMLElement
        {
            addElementToWrapper(this,'input') as HTMLInputElement;
            element.setAttribute('type', 'text');
            
            addEventListener(MouseEvent.CLICK, clickHandler);
            
            return element;
        }

        COMPILE::JS
        protected function clickHandler(event:MouseEvent):void
		{
            console.log("textinput clicked");
        }
    }
}

Then used:

<component:Tester/>

<j:Button text="add focus listener" click="addListenerToTester(event)"/>

and

private function addListenerToTester(event:Event):void
{
    COMPILE::JS
    {
    console.log("adding listener");
    event.target.element.addEventListener('focus', onInputFocus)
    }
}
private function onInputFocus(event:Event):void
{
    COMPILE::JS
    {
    console.log("onInputFocus");
    }
}

carlosrovira avatar Dec 11 '20 16:12 carlosrovira

@raudjcholo Can this be closed?

estanglerbm avatar Jul 25 '21 11:07 estanglerbm