Web AppBuilder Developer Edition - on(someNode, "click", someFunction) Executes N Times When Widget Reopened for N Times

671
3
Jump to solution
05-18-2021 01:05 PM
XuanKuai
New Contributor III

Hi,

I have an on(someNode, "click", someFunction) in the onOpen() of Widget.js. The problem is that if the widget is closed then reopened (2nd open), the click event on someNode fires twice. If close and reopen the widget again (3rd open), the click event fires three times, so on and so forth...

Is there a way to let the click event on someNode only fire once after reopening the widget?

I appreciate any help. Thanks!

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Put that into the postCreate function instead of the onOpen function.

View solution in original post

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

Put that into the postCreate function instead of the onOpen function.

0 Kudos
XuanKuai
New Contributor III

Yes! This solves the issue. May I know why the onOpen() duplicates the event handler but the postCreate() does not?

0 Kudos
KenBuja
MVP Esteemed Contributor

The Widget life cycle page in the documentation might help explain this. The postCreate method happens only once when the widget is first created. The onOpen method happens every time the widget is opened. Since your on event was added during onOpen, every time the widget is opened, another event listener is added.

The other way to make sure the event is only added once would be to remove your on event in the onClose method.

0 Kudos