- Ext JS 4 Plugin and Extension Development
- Abdullah Al Mohammad
- 489字
- 2021-08-06 16:56:34
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:
Ext.define('Examples.plugin.Sms', { alias : 'plugin.sms', config : { perMessageLength : 160, defaultColor : '#000000', warningColor : '#ff0000' }, constructor : function(cfg) { Ext.apply(this, cfg); this.callParent(arguments); }, init : function(textField) { this.textField = textField; if (!textField.rendered) { textField.on('afterrender', this.handleAfterRender, this); } else { this.handleAfterRender(); } }, handleAfterRender : function() { this.textField.on({ scope : this, change : this.handleChange }); var dom = Ext.get(this.textField.bodyEl.dom); Ext.DomHelper.append(dom, { tag : 'div', cls : 'plugin-sms' }); }, handleChange : function(field, newValue) { if (newValue.length > this.getPerMessageLength()) { field.setFieldStyle('color:' + this.getWarningColor()); } else { field.setFieldStyle('color:' + this.getDefaultColor()); } this.updateMessageInfo(newValue.length); }, updateMessageInfo : function(length) { var tpl = ['Characters: {length}<br/>', 'Messages:{messages}'].join(''); var text = new Ext.XTemplate(tpl); var messages = parseInt(length / this.getPerMessageLength()); if ((length / this.getPerMessageLength()) - messages > 0) { ++messages; } Ext.get(this.getInfoPanel()).update(text.apply({ length : length, messages : messages })); }, getInfoPanel : function() { return this.textField.el.select('.plugin-sms'); } });
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:
{ xtype : 'textareafield', plugins : ['sms'] }
Also, we can supply configuration options when we are inserting the plugin within the plugins
configuration option to override the default values, as follows:
plugins : [Ext.create('Examples.plugin.Sms', { perMessageLength : 20, defaultColor : '#0000ff', warningColor : "#00ff00" })]
- CockroachDB權(quán)威指南
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- 現(xiàn)代C++編程實(shí)戰(zhàn):132個(gè)核心技巧示例(原書第2版)
- Extending Unity with Editor Scripting
- Visual Basic程序設(shè)計(jì)全程指南
- 貫通Tomcat開發(fā)
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- Python大規(guī)模機(jī)器學(xué)習(xí)
- PHP 8從入門到精通(視頻教學(xué)版)
- 面向?qū)ο蟪绦蛟O(shè)計(jì)及C++(第3版)
- Python應(yīng)用與實(shí)戰(zhàn)
- Python數(shù)據(jù)科學(xué)實(shí)踐指南
- HikariCP數(shù)據(jù)庫(kù)連接池實(shí)戰(zhàn)
- 你必須知道的.NET(第2版)
- MySQL核心技術(shù)與最佳實(shí)踐