SAPUI5 Remove Event Listeners from Controls

0
3198

When an action is performed in UI5, a connected Event is fired using Event relevant Handlers. Following tutorial shows how to correctly remove Event Listeners from the Control, in cases where Event Listeners are required further no more on the screen.

Event handler

Let try our hands on removing the Event Listener from the Button Press. The code below does that for you.

Sample Code:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4. <title>SAPUI5 Removing Event Listeners from Control</title>
  5. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”/>
  6. <meta http-equiv=“X-UA-Compatible” content=“IE=edge”/>
  7. <script id=“sap-ui-bootstrap”
  8. type=“text/javascript”
  9. src=“https://openui5.hana.ondemand.com/1.36.12/resources/sap-ui-core.js”
  10. data-sap-ui-theme=“sap_bluecrystal”
  11. data-sap-ui-libs=“sap.ui.commons”>
  12. </script>
  13. <script type=“text/javascript”>
  14. var oBtn, fnBtnPressHandler;
  15. fnBtnPressHandler = function (oEvent) {
  16. var src;
  17. src = oEvent.getSource();
  18. //Check if Event is for button
  19. if (src instanceof sap.ui.commons.Button) {
  20. alert(“Removing the handler function on button press”);
  21. src.detachPress(fnBtnPressHandler);
  22. }
  23. };
  24. oBtn = new sap.ui.commons.Button({
  25. text : “detachButtonPress”
  26. });
  27. oBtn.attachPress(fnBtnPressHandler);
  28. oBtn.placeAt(“content”);
  29. </script>
  30. </head>
  31. <body class=“sapUiBody” role=“application”>
  32. <div id=“content”></div>
  33. </body>
  34. </html>

LEAVE A REPLY