- 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" })]
- DB2 V9權(quán)威指南
- C語(yǔ)言程序設(shè)計(jì)立體化案例教程
- 從學(xué)徒到高手:汽車(chē)電路識(shí)圖、故障檢測(cè)與維修技能全圖解
- Learning R for Geospatial Analysis
- 軟件測(cè)試實(shí)用教程
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- 圖數(shù)據(jù)庫(kù)實(shí)戰(zhàn)
- Mastering Web Application Development with AngularJS
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級(jí)賬本看區(qū)塊鏈架構(gòu)設(shè)計(jì)
- Java 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- iOS開(kāi)發(fā)項(xiàng)目化入門(mén)教程
- Mastering Bootstrap 4
- Java多線(xiàn)程并發(fā)體系實(shí)戰(zhàn)(微課視頻版)
- Oracle SOA Suite 12c Administrator's Guide
- 小學(xué)生C++趣味編程從入門(mén)到精通