Rene Padillo
Rene Padillo

Reputation: 2358

How to set event listeners in Cordova plugin

I would like to ask on how you can set your own event listener in your Cordova plugin.

I have this Share dialog for my Android and I wanted to have my Javascript to set a listener for onShareDialogDismiss or onShareDialogLaunched

What would likely to happen in Javascript would look like this.

// Set a listener for dialog dismiss
document.addEventListener('onShareDialogDismiss', listenerCallback, false);

// Set a listener for dialog launch
document.addEventListener('onShareDialogLaunch', launchCallback, false);

I have this code from Titanium, but it's using TiViewProxy class, would likely to know how you could do an alternative for fireEvent() in pure Android implementation

Thanks!

Upvotes: 6

Views: 4376

Answers (1)

beaver
beaver

Reputation: 17647

Firstly have you already read Cordova plugin development documentation?

Then you can see how a plugin like this cordova-plugin-network-information is done :

Check for example the JS interface code in which there are these code lines to raise a Document event:

cordova.fireDocumentEvent("offline");

or

cordova.fireDocumentEvent("online");

Reading inside cordova.js there is a minimal documentation for this API:

/**
 * Method to fire event from native code
 * bNoDetach is required for events which cause an exception which needs to be caught in native code
 */
fireDocumentEvent: function(type, data, bNoDetach)

Another API available is fireWindowEvent: function(type, data), but you can find out other APIs reading directly inside cordova.js.

Upvotes: 6

Related Questions