Building an Ext JS plugin
Let us start developing an Ext JS plugin. In this section we will develop a simple SMS plugin, targeting the Ext JS textareafield
component. The feature we wish to provide for the SMS functionality is that it should show the number of characters and the number of messages on the bottom of the containing field. Also, the color of the text of the message should change in order to notify the users whenever they exceed the allowed length for a message.
Here, in the following code, the SMS plugin class has been created within the Examples
namespace of an Ext JS application:
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
In the preceding plugin class, you can see that within this class we have defined a "must implemented" function called init
. Within the init
function, we check whether the component, on which this plugin is attached, has rendered or not, and then call the handleAfterRender
function whenever the rendering is. Within this function, a code is provided, such that when the change
event fires off the
textareafield
component, the handleChange
function of this class should get executed; simultaneously, create an HTML <div>
element within the handleAfterRender
function, where we want to show the message information regarding the characters and message counter. And the handleChange
function is the handler that calculates the message length in order to show the colored, warning text, and call the updateMessageInfo
function to update the message information text for the characters length and the number of messages.
Now we can easily add the following plugin to the component:
Also, we can supply configuration options when we are inserting the plugin within the plugins
configuration option to override the default values, as follows: