CustomEvent Usage

nativescriptcore

#1

Hello,

I have trouble using Custom Events in NativeScript, like described here. I am using NativeScript + Angular.

I’m new to the framework and languages, so I might miss something obvious here, but I’ve been trying for days now.

I have a dataclass, that holds some status information about the app.

Status class

export class Status extends observableModule.Observable {
private _myProperty: number = 1;

public get myProperty(): number {
    return this._myProperty;
}

public set myProperty(value: number) {
    this._myProperty = value;
    //this works, but is pretty useless for me
    this.addEventListener("myCustomEvent", function(eventData){
         console.log(eventData.eventName + " has been raised! by: " + eventData.object);
    } );
    let eventData: observableModule.EventData = {
        eventName: "myCustomEvent",
        object: this
      }
      this.notify(eventData);
}

}

When a property in this class changes I want to update my UI to display different navigation options, so I need to call a function from my *.component.ts class.

How do I register a listener, that reacts to my “myCustomEvent” or how do I pass a callback function from my *.component.ts class to the working listener inside the status class?

Registering listener for a propertyChanged Event would work aswell, but I’ve also had no success with that.

Thank you for helping :slight_smile:


#2

It should work as described in the docs. Here is one example I recently built for another forum user, as you see I’m triggering and listening to custom event on a page.