Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom event to a NativeScript UI plugin

What is required to define a custom event for a UI plugin in NativeScript?

What I'm trying to achieve is to trigger a foo event that works similar to the tap event on a Button and can be hooked into as follows:

<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:fooplugin="nativescript-foo-plugin">
  <StackLayout>
    <Button text="Tap Me!" tap="{{ onTap }}" />
    <fooplugin:FooPlugin foo="{{ onFoo }}" />
  </StackLayout>
</Page>

What I've done essentially boils down to calling the notify function with the eventName value of foo from within the plugin code (ignoring memory leak considerations):

import * as view from 'ui/core/view';

export class FooPlugin extends view.View {
  constructor() {
    super();

    setTimeout(() => {
      this.notify({
        eventName: 'foo',
        object: this,
      });

      // also tried this._emit('foo');
    }, 1000);
  }
}

Is there something else that I'm missing and that I need to do to make this work?

like image 896
Merott Avatar asked Feb 07 '23 06:02

Merott


1 Answers

create a property public static fooEvent="foo" the name of the property is important it should be eventname+ Event now it should work.

like image 157
Habib Kazemi Avatar answered Feb 08 '23 19:02

Habib Kazemi