How to broadcast and handling custom event in nativescript?

nativescriptcore

#1

I want to create a custom event and use this event


#2

Guys the documentation has everything you need and it has a embedded Google Search too, make use of them.

https://docs.nativescript.org/core-concepts/events#creating-a-custom-event


#3

I tried but didnt got the sollution. Can you give me the sample code


#4

For example, lets assume you want to fire a custom event on one of your StackLayout

XML

<StackLayout id="stackLayout" class="home-panel">
</StackLayout>

JS

    var target = page.getViewById('stackLayout');
    target.on('myCustomEvent', onMyCustomEvent);
    target.notify({
        eventName: 'myCustomEvent',
        object: target
    });
....
....

function onMyCustomEvent(args) {
    alert('My custom eventaaa works!!!');
}