Own Module | Call JavaScript code on click

Hello,

i am working on an own Iciang Web 2 Module. Now i have problems with a quite simple thing. Or at least i thought it is simple.

In the view script of the module i want to define a text link which calls a JavaScript function (and do nothing more).

Remarks: The module has autorefresh activated.

The ways i tried it:

  • Write onclick parameter in view script -> Is overwritten by Icinga Web 2 with its own click event.
  • The url of the link get extra GET parameter, e.g.: execute_js=yes&uuid=XXXXX.
    • The idea is that the controller can save the uuid from the last time before it has autorefreshed and knows then, if the parameter execute_js=yes is fresh or note.
    • The problem here is, that the instance of the module controller is reinitializing on every autorefresh. So i can not store an old state in the instance.
  • The JavaScript class in module.js is not reinitializing on every cycle:
    • Rendered: is called on every render cycle
    • initalized: is called only on initialisation.
    • Thats fine, but what i need: a Function that is called on a specific click event.

Can someone give me a hint what i can try?

Best Regards

Matze

Hi,

since you already have a module.js and an appropriate object in there, you should register (during initialisation) an event listener for click on the link you want to catch. (e.g. this.on('click', '.yourlink', this.onClick))

The link you’re rendering should have the attribute data-no-icinga-ajax which prevents Icinga from handling the link click itself in recent versions of Icinga Web 2. You then have full control over what should happen when clicking your link.