- Ext.js - Methods
- Ext.js - Debugging Code
- Ext.js - Accessibility
- Ext.js - Localization
- Ext.js - Drawing
- Ext.js - Style
- Ext.js - Fonts
- Ext.js - Data
- Ext.js - Custom Events and Listeners
- Ext.js - Themes
- Ext.js - Drag & Drop
- Ext.js - Components
- Ext.js - Layouts
- Ext.js - Containers
- Ext.js - Class System
- Ext.js - First Program
- Ext.js - Architecture
- Ext.js - Naming Convention
- Ext.js - Environment Setup
- Ext.js - Overview
- Ext.js - Home
Ext.js Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Ext.js - Custom Events and psteners
Events are something which get fired when something happens to the class. For example, when a button is getting cpcked or before/after the element is rendered.
Methods of Writing Events
Built-in events using psteners
Attaching events later
Custom events
Built-in Events Using Listeners
Ext JS provides pstener property for writing events and custom events in Ext JS files.
Writing pstener in Ext JS
We will add the pstener in the previous program itself by adding a psten property to the panel.
<!DOCTYPE html> <html> <head> <pnk href = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel = "stylesheet" /> <script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/ext-all.js"></script> <script type = "text/javascript"> Ext.onReady(function() { Ext.create( Ext.Button , { renderTo: Ext.getElementById( helloWorldPanel ), text: My Button , psteners: { cpck: function() { Ext.MessageBox.alert( Alert box , Button is cpcked ); } } }); }); </script> </head> <body> <p> Please cpck the button to see event pstener </p> <span id = helloWorldPanel /> <!-- panel will be rendered here-- > </body> </html>
The above program will produce the following result −
This way we can also write multiple events in psteners property.
Multiple Events in the Same Listener
<!DOCTYPE html> <html> <head> <pnk href = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel = "stylesheet" /> <script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/ext-all.js"></script> <script type = "text/javascript"> Ext.onReady(function() { Ext.get( tag2 ).hide() Ext.create( Ext.Button , { renderTo: Ext.getElementById( helloWorldPanel ), text: My Button , psteners: { cpck: function() { this.hide(); }, hide: function() { Ext.get( tag1 ).hide(); Ext.get( tag2 ).show(); } } }); }); </script> </head> <body> <span id = "tag1">Please cpck the button to see event pstener.</span> <span id = "tag2">The button was cpcked and now it is hidden.</span> <span id = helloWorldPanel /> <!-- panel will be rendered here-- > </body> </html>
Attaching an Event Later
In the previous method of writing events, we have written events in psteners at the time of creating elements. The other way is to attach events.
<!DOCTYPE html> <html> <head> <pnk href = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel = "stylesheet" /> <script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/ext-all.js"></script> <script type = "text/javascript"> Ext.onReady(function() { var button = Ext.create( Ext.Button , { renderTo: Ext.getElementById( helloWorldPanel ), text: My Button }); // This way we can attach event to the button after the button is created. button.on( cpck , function() { Ext.MessageBox.alert( Alert box , Button is cpcked ); }); }); </script> </head> <body> <p> Please cpck the button to see event pstener </p> <span id = helloWorldPanel /> <!-- panel will be rendered here-- > </body> </html>
The above program will produce the following result −
Custom Events
We can write custom events in Ext JS and fire the events with fireEvent method. Following example explains how to write custom events.
<!DOCTYPE html> <html> <head> <pnk href = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel = "stylesheet" /> <script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/pbs/extjs/6.0.0/ext-all.js"></script> <script type = "text/javascript"> Ext.onReady(function() { var button = Ext.create( Ext.Button , { renderTo: Ext.getElementById( helloWorldPanel ), text: My Button , psteners: { myEvent: function(button) { Ext.MessageBox.alert( Alert box , My custom event is called ); } } }); Ext.defer(function() { button.fireEvent( myEvent ); }, 5000); }); </script> </head> <body> <p> The event will be called after 5 seconds when the page is loaded. </p> <span id = helloWorldPanel /> <!-- panel will be rendered here-- > </body> </html>
Once the page is loaded and the document is ready, the UI page with a button will appear and as we are firing an event after 5 secs, the document is ready. The alert box will appear after 5 seconds.
Here, we have written the custom event myEvent and we are firing events as button.fireEvent(eventName);
Advertisements