SlideShare a Scribd company logo
Developing Components
and Extensions for Ext JS
2010 Mats Bryntse
About me
{
name : ”Mats Bryntse”,
extForumAlias: ’mankz’,
age : 33,
from: ”Helsingborg, Sweden”,
usingExtSince : 2007,
creatorOf: [”Ext Scheduler”, ”Ext Gantt”],
twitter : ”@extscheduler”
}
Agenda
* What is an Ext extension?
* Extension vs override vs plugin.
* Solve a simple silly problem in 3 ways
* Create a clock plugin, Ext.ux.Clock
* 10 Do’s and Dont’s when creating a
UX
What is an Ext JS
extension?
An extension is a reusable component,
normally derived from an existing Ext JS class.
Let’s look at some popular community
extensions.
Some popular community
extensions
// By Saki
Ext.ux.form.LovCombo = Ext.extend(Ext.form.ComboBox, {
});
// By MindPatterns
Ext.ux.grid.livegrid.GridPanel =
Ext.extend(Ext.grid.GridPanel, { });
// By Condor
Ext.ux.data.PagingStore = Ext.extend(Ext.data.Store, { });
Extensions don’t have to involve UI.
Terminology
* Extension : Create a new class with added
or modified behavior
* Override : Globally alter the behavior of an
existing class (useful for patching etc).
* Plugin : Augment and add behavior to an
Ext.Component instance (but not tied to a class)
Buttons that explode
when clicked are
way cool !!!
Real world scenario
Client:
Client is always right
3 ways of solving this ”real world
problem”:
* Create an extension (a new class)
* Override Ext.Button globally
* Create a plugin
Let’s create a simple
extension!
Using Ext.Button as the base class
seems reasonable.
First let’s take a look at Ext.extend
Ext.extend
Ext.extend(Function superclass, Object overrides
) : Function
* The overrides end up on the prototype of your
new class (shared between all instances).
PuffButton extension
PuffButton = Ext.extend(Ext.Button, {
constructor: function(config) {
// Remember to call base class method
PuffButton.superclass.constructor.apply(this,
arguments);
// Add listener for the button ’click’ event
this.on('click', function() { this.el.puff(); }, this);
}
});
Let’s try it out in Firebug!
PuffButton = Ext.extend(Ext.Button, {
constructor: function(config) {
// Must call base class method
PuffButton.superclass.constructor.apply(this, arguments);
// Add listener for the button ’click’ event
this.on('click', function() { this.el.puff(); }, this);
}
});
new PuffButton ({width:130, text: "Puff", renderTo :
Ext.getBody()});
Ext.extend review
We extended an existing Ext class to
create our own class encapsulating new
behaviour.
Let’s do the same with an
override
// Will affect all Buttons globally
Ext.override(Ext.Button, {
onClick :
Ext.Button.prototype.onClick.createSequence(function(){
this.el.puff();
})
});
new Ext.Button ({width : 130, text: "Override Puff", renderTo :
Ext.getBody()});
Ext.override review
* We used Ext.override to alter the
behavior of an existing class.
* Any instances created before or after
our override are affected.
Last step, let’s do the
same with a plugin
A plugin is any type of object that has
an init method.
var myPlugin = {
init : function(cmp) {
alert(’Hello world’);
}
};
Let’s do the same with a
plugin
Puffable = function() {
this.init = function(cmp) {
cmp.on("afterrender", function() {
this.el.on("click", this.el.puff, this.el);
});
};
};
// Augment a button
new Ext.Button ({text: "Plugin Puff", renderTo : Ext.getBody(),
plugins : new Puffable() });
// Augment a Panel
new Ext.Panel({ height : 300, width: 300, title : "Puff Plugin",
renderTo : Ext.getBody(), plugins : new Puffable()});
Plugin review
We used the plugin concept to add
functionality to a single instance of an
existing class.
Note: The plugin itself is not tied to a
specific class (though will only work on
an Ext.Component)
Let’s create something
useful instead
Goal: Create an analog clock extension.
We’ll use Raphael to visualize the clock
hands
Download the source from the Ext forums:
http://www.sencha.com/forum/showthread.php?115907
Step 1 – Choose a
suitable base class
* We want to be able to use the clock inside a
Panel or Window etc. => Ext.Component.
* We want the clock to be able to have any size
=> Ext.BoxComponent
* We don’t really need support for toolbars,
headers, buttons etc. => Ext.Panel.
Introduction to
Ext.BoxComponent
* Base class of most UI widgets in Ext JS
(GridPanel, TabPanel, TextField etc...)
* Base class for any Component that is to
be sized as a box, using width and height.
Ext.Component Life
Cycle & Template
Methods
* Initialization (constructor, initComponent)
- Configuration, setup etc...
* Rendering (onRender, afterRender)
- Add additional elements and styling here
* Destruction (onDestroy)
- Clean up after yourself, destroy elements etc.
Step 2 – Create folders
and a simple skeleton
Step 3 – Create a simple
skeleton with stubs
Ext.ns('Ext.ux');
Ext.ux.Clock = Ext.extend(Ext.BoxComponent, {
afterRender : function() {
// Call superclass
Ext.ux.Clock.superclass.afterRender.apply(this, arguments);
},
onDestroy : function() {
// Call superclass
Ext.ux.Clock.superclass.onDestroy.apply(this, arguments);
}
});
ext.ux.clock.js
Step 4 – Create simple
example HTML page
<html>
<head>
<!--Ext lib and UX components-->
...
<script type="text/javascript">
Ext.onReady(function(){
var clock = new Ext.ux.Clock({
height:150,
width:150
});
clock.render(Ext.getBody());
});
</script>
</head>
index.html
Step 5 – Create elements
afterRender : function() { // The component is now rendered and has an ’el’
var size = Math.min(this.getHeight(), this.getWidth());
// Background image of an empty clock with no hands
this.bgEl = this.el.createChild({
tag : 'img',
cls : 'ext-ux-clock-img',
src : this.clockBgUrl,
width : size,
height : size
});
// Initialize a Raphael canvas for drawing the hands
this.canvas = Raphael(this.el.dom, size, size);
this.drawHands();
this.on('resize', this.handleResize, this);
this.timer = setInterval(this.drawHands.createDelegate(this), 1000);
Ext.ux.Clock.superclass.afterRender.apply(this, arguments);
}
Step 6 – Draw hands
drawHands : function() {
var size = Math.min(this.getHeight(), this.getWidth())
date = new Date(),
secs = date.getSeconds(),
mins = date.getMinutes(),
hrs = date.getHours(),
canvas = this.canvas;
canvas.clear();
canvas.path(...); // Draw minute hand
canvas.path(...); // Draw hour hand
canvas.path(...); // Draw second hand
}
Let’s run it
Step 7 – Use a
background image
Step 8 – Polish with
CSS3
.ext-ux-clock-img
{
border:3px solid lightgrey;
-moz-border-radius:100%;
-webkit-border-radius: 100%;
-o-border-radius: 100%;
border-radius: 100%;
-moz-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8);
-webkit-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8);
-o-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8);
box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8);
background:#222333 url(../images/glow.png) no-repeat center center;
}
Step 8 – Polished result
Step 9 – Resize Support
handleResize : function(me, newWidth, newHeight) {
var size = Math.min(newWidth, newHeight);
this.bgEl.setSize(size, size, true); // true to animate
this.canvas.setSize(size, size); // Resize Raphael canvas
this.drawHands(); // Clears canvas and redraws
}
Let’s make sure the clock is resizable.
Step 9 – Let’s try out the
resizing in an
Ext.Window
Step 10 – Don’t forget to
clean up after yourself!
onDestroy : function() {
clearInterval(this.timer);
this.canvas.clear();
Ext.destroy(this.bgImg, this.innerEl);
// Call superclass
Ext.ux.Clock.superclass.onDestroy.apply(this, arguments);
}
Bonus step: World Clock
10 Do’s and Don’ts when
creating an Ext extension
Here is a list of some things to think
about when creating your extension.
10
?Why?
! Other developers will have a better chance of
understanding (and maintaining) your code.
Additionally, the Ext JS source contains lots of
best practices.
1. Follow Ext JS coding
patterns
var w = 100;
var h = 40;
var s = 0;
if (doCalculate)
s = w * h;
var width = 100,
height = 40,
area = 0;
if (doCalculate) {
area = width *
height;
}
1. Follow Ext JS coding
patterns
2. Design classes for
configurability
?Why?
! It will allow your class to be easily configured
without the use of huge overrides. This concept is
seen throughout all of Ext JS.
MyTip = Ext.extend(Ext.Tooltip, {
onMouseLeave: function(){
this.el.fadeOut(200);
}
}
2. Design classes for
configurability
MyTip = Ext.extend(Ext.Tooltip, {
fadeDuration: 200,
onMouseLeave : function(){
this.el.fadeOut(this.fadeDuration);
}
}
3. Make key functionality
easily overridable
?Why?
! It will allow your class to be easily altered
without the use of huge overrides. This concept is
seen throughout all of Ext JS.
initComponent : function(){
this.tpl = new Ext.XTemplate(
”<div>{foo}</div>”
);
// ....
}
3. Make key functionality
easily overridable
initComponent : function(){
if (!this.tpl) {
this.tpl = new Ext.XTemplate(
'<div>{foo}</div>”
);
}
// ....
}
4. Make classes
localizable
?Why?
! Because you know your boss will ask about
localization support at some point.
MyClass = Ext.extend(Ext.Toolbar, {
constructor: function() {
this.add({
text : 'No data to display’
});
....
});
4. Make classes
localizable
MyClass = Ext.extend(Ext.Toolbar, {
noDataText : 'No data to display’,
constructor: function() {
this.add({
text : this.noDataText
});
});
});
5. Use a syntax checker
?Why?
! Helps you find global variable leaks, extra
commas etc. Use JsLint or JavaScriptLint
(beware, JsLint WILL hurt your feelings).
?Why?
! Because noone likes memory leaks. Override
the onDestroy method to clean up any additional
elements, event listeners etc...
6. Clean up after yourself
MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
this.someEl = new
Ext.Element();
},
....
});
MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
this.someEl = new Ext.Element();
},
onDestroy: function() {
this.someEl.destroy();
// Call superclass destroy method...
}
});
6. Clean up after yourself
?Why?
! Because you (or someone else) may want to
make use of the lazy instantiation mechanism
provided by Ext.
7. Define an xtype
MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
// ...
}
});
MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
// ...
}
});
Ext.reg(’mypanel’, MyPanel);
7. Define an xtype
?Why?
! Because other developers will likely read your
code. By using the JSDoc syntax you can
generate beautiful documentation looking like the
Ext online API (using Ext-Doc).
8. Document your
extension
MyClass =
Ext.extend(Ext.Panel, {
// ...
});
/**
* @class MyClass
* @extends Ext.Panel
* @constructor
* @param {Object} config The cfg
object
*/
MyClass = Ext.extend(Ext.Panel, {
// ...
});
8. Document your
extension
?Why?
!Noone likes bugs. Some examples:
* What happens if you include multiple instances
of your extension?
* What happens when it’s destroyed? Any leaked
DOM nodes etc?
9. Test edge cases
?Why?
! You might not care, but everyone that wants to
use your extension in a production environment
will (should) care.
10. Include a license
Additional resources
• Sencha Learning Center:
http://www.sencha.com/learn/Ext_2_Overview
• Saki’s Blog:
http://blog.extjs.eu/know-how/extension-or-
plugin/
Questions?
mats@ext-scheduler.com

More Related Content

Similar to SenchaCon 2010: Developing components and extensions for ext js

Creating Ext JS Extensions and Components
Creating Ext JS Extensions and ComponentsCreating Ext JS Extensions and Components
Creating Ext JS Extensions and Components
Sencha
 
Developing components and extensions for ext js
Developing components and extensions for ext jsDeveloping components and extensions for ext js
Developing components and extensions for ext jsMats Bryntse
 
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
Sencha
 
Four Ways to add Features to Ext JS
Four Ways to add Features to Ext JSFour Ways to add Features to Ext JS
Four Ways to add Features to Ext JSShea Frederick
 
Ext oo
Ext ooExt oo
Ext oo
o52tiger
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)
Lifeparticle
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JS
Dominik Jungowski
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
Sencha
 
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and ToolingSencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
Mindfire Solutions
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 MinigamesSusan Gold
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
Daroko blog(www.professionalbloggertricks.com)
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorialsumitjoshi01
 
Gui
GuiGui

Similar to SenchaCon 2010: Developing components and extensions for ext js (20)

Creating Ext JS Extensions and Components
Creating Ext JS Extensions and ComponentsCreating Ext JS Extensions and Components
Creating Ext JS Extensions and Components
 
Developing components and extensions for ext js
Developing components and extensions for ext jsDeveloping components and extensions for ext js
Developing components and extensions for ext js
 
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
 
Four Ways to add Features to Ext JS
Four Ways to add Features to Ext JSFour Ways to add Features to Ext JS
Four Ways to add Features to Ext JS
 
Ext oo
Ext ooExt oo
Ext oo
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JS
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
 
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and ToolingSencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
 
Ext JS Introduction
Ext JS IntroductionExt JS Introduction
Ext JS Introduction
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
ExtJS framework
ExtJS frameworkExtJS framework
ExtJS framework
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Custom components
Custom componentsCustom components
Custom components
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
Gui
GuiGui
Gui
 

Recently uploaded

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

SenchaCon 2010: Developing components and extensions for ext js

  • 1. Developing Components and Extensions for Ext JS 2010 Mats Bryntse
  • 2. About me { name : ”Mats Bryntse”, extForumAlias: ’mankz’, age : 33, from: ”Helsingborg, Sweden”, usingExtSince : 2007, creatorOf: [”Ext Scheduler”, ”Ext Gantt”], twitter : ”@extscheduler” }
  • 3. Agenda * What is an Ext extension? * Extension vs override vs plugin. * Solve a simple silly problem in 3 ways * Create a clock plugin, Ext.ux.Clock * 10 Do’s and Dont’s when creating a UX
  • 4. What is an Ext JS extension? An extension is a reusable component, normally derived from an existing Ext JS class. Let’s look at some popular community extensions.
  • 5. Some popular community extensions // By Saki Ext.ux.form.LovCombo = Ext.extend(Ext.form.ComboBox, { }); // By MindPatterns Ext.ux.grid.livegrid.GridPanel = Ext.extend(Ext.grid.GridPanel, { }); // By Condor Ext.ux.data.PagingStore = Ext.extend(Ext.data.Store, { }); Extensions don’t have to involve UI.
  • 6. Terminology * Extension : Create a new class with added or modified behavior * Override : Globally alter the behavior of an existing class (useful for patching etc). * Plugin : Augment and add behavior to an Ext.Component instance (but not tied to a class)
  • 7. Buttons that explode when clicked are way cool !!! Real world scenario Client:
  • 8. Client is always right 3 ways of solving this ”real world problem”: * Create an extension (a new class) * Override Ext.Button globally * Create a plugin
  • 9. Let’s create a simple extension! Using Ext.Button as the base class seems reasonable. First let’s take a look at Ext.extend
  • 10. Ext.extend Ext.extend(Function superclass, Object overrides ) : Function * The overrides end up on the prototype of your new class (shared between all instances).
  • 11. PuffButton extension PuffButton = Ext.extend(Ext.Button, { constructor: function(config) { // Remember to call base class method PuffButton.superclass.constructor.apply(this, arguments); // Add listener for the button ’click’ event this.on('click', function() { this.el.puff(); }, this); } });
  • 12. Let’s try it out in Firebug! PuffButton = Ext.extend(Ext.Button, { constructor: function(config) { // Must call base class method PuffButton.superclass.constructor.apply(this, arguments); // Add listener for the button ’click’ event this.on('click', function() { this.el.puff(); }, this); } }); new PuffButton ({width:130, text: "Puff", renderTo : Ext.getBody()});
  • 13. Ext.extend review We extended an existing Ext class to create our own class encapsulating new behaviour.
  • 14. Let’s do the same with an override // Will affect all Buttons globally Ext.override(Ext.Button, { onClick : Ext.Button.prototype.onClick.createSequence(function(){ this.el.puff(); }) }); new Ext.Button ({width : 130, text: "Override Puff", renderTo : Ext.getBody()});
  • 15. Ext.override review * We used Ext.override to alter the behavior of an existing class. * Any instances created before or after our override are affected.
  • 16. Last step, let’s do the same with a plugin A plugin is any type of object that has an init method. var myPlugin = { init : function(cmp) { alert(’Hello world’); } };
  • 17. Let’s do the same with a plugin Puffable = function() { this.init = function(cmp) { cmp.on("afterrender", function() { this.el.on("click", this.el.puff, this.el); }); }; }; // Augment a button new Ext.Button ({text: "Plugin Puff", renderTo : Ext.getBody(), plugins : new Puffable() }); // Augment a Panel new Ext.Panel({ height : 300, width: 300, title : "Puff Plugin", renderTo : Ext.getBody(), plugins : new Puffable()});
  • 18. Plugin review We used the plugin concept to add functionality to a single instance of an existing class. Note: The plugin itself is not tied to a specific class (though will only work on an Ext.Component)
  • 19. Let’s create something useful instead Goal: Create an analog clock extension. We’ll use Raphael to visualize the clock hands Download the source from the Ext forums: http://www.sencha.com/forum/showthread.php?115907
  • 20. Step 1 – Choose a suitable base class * We want to be able to use the clock inside a Panel or Window etc. => Ext.Component. * We want the clock to be able to have any size => Ext.BoxComponent * We don’t really need support for toolbars, headers, buttons etc. => Ext.Panel.
  • 21. Introduction to Ext.BoxComponent * Base class of most UI widgets in Ext JS (GridPanel, TabPanel, TextField etc...) * Base class for any Component that is to be sized as a box, using width and height.
  • 22. Ext.Component Life Cycle & Template Methods * Initialization (constructor, initComponent) - Configuration, setup etc... * Rendering (onRender, afterRender) - Add additional elements and styling here * Destruction (onDestroy) - Clean up after yourself, destroy elements etc.
  • 23. Step 2 – Create folders and a simple skeleton
  • 24. Step 3 – Create a simple skeleton with stubs Ext.ns('Ext.ux'); Ext.ux.Clock = Ext.extend(Ext.BoxComponent, { afterRender : function() { // Call superclass Ext.ux.Clock.superclass.afterRender.apply(this, arguments); }, onDestroy : function() { // Call superclass Ext.ux.Clock.superclass.onDestroy.apply(this, arguments); } }); ext.ux.clock.js
  • 25. Step 4 – Create simple example HTML page <html> <head> <!--Ext lib and UX components--> ... <script type="text/javascript"> Ext.onReady(function(){ var clock = new Ext.ux.Clock({ height:150, width:150 }); clock.render(Ext.getBody()); }); </script> </head> index.html
  • 26. Step 5 – Create elements afterRender : function() { // The component is now rendered and has an ’el’ var size = Math.min(this.getHeight(), this.getWidth()); // Background image of an empty clock with no hands this.bgEl = this.el.createChild({ tag : 'img', cls : 'ext-ux-clock-img', src : this.clockBgUrl, width : size, height : size }); // Initialize a Raphael canvas for drawing the hands this.canvas = Raphael(this.el.dom, size, size); this.drawHands(); this.on('resize', this.handleResize, this); this.timer = setInterval(this.drawHands.createDelegate(this), 1000); Ext.ux.Clock.superclass.afterRender.apply(this, arguments); }
  • 27. Step 6 – Draw hands drawHands : function() { var size = Math.min(this.getHeight(), this.getWidth()) date = new Date(), secs = date.getSeconds(), mins = date.getMinutes(), hrs = date.getHours(), canvas = this.canvas; canvas.clear(); canvas.path(...); // Draw minute hand canvas.path(...); // Draw hour hand canvas.path(...); // Draw second hand }
  • 29. Step 7 – Use a background image
  • 30. Step 8 – Polish with CSS3 .ext-ux-clock-img { border:3px solid lightgrey; -moz-border-radius:100%; -webkit-border-radius: 100%; -o-border-radius: 100%; border-radius: 100%; -moz-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8); -webkit-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8); -o-box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8); box-shadow:1px 1px 13px rgba(114, 114, 114, 0.8); background:#222333 url(../images/glow.png) no-repeat center center; }
  • 31. Step 8 – Polished result
  • 32. Step 9 – Resize Support handleResize : function(me, newWidth, newHeight) { var size = Math.min(newWidth, newHeight); this.bgEl.setSize(size, size, true); // true to animate this.canvas.setSize(size, size); // Resize Raphael canvas this.drawHands(); // Clears canvas and redraws } Let’s make sure the clock is resizable.
  • 33. Step 9 – Let’s try out the resizing in an Ext.Window
  • 34. Step 10 – Don’t forget to clean up after yourself! onDestroy : function() { clearInterval(this.timer); this.canvas.clear(); Ext.destroy(this.bgImg, this.innerEl); // Call superclass Ext.ux.Clock.superclass.onDestroy.apply(this, arguments); }
  • 36. 10 Do’s and Don’ts when creating an Ext extension Here is a list of some things to think about when creating your extension. 10
  • 37. ?Why? ! Other developers will have a better chance of understanding (and maintaining) your code. Additionally, the Ext JS source contains lots of best practices. 1. Follow Ext JS coding patterns
  • 38. var w = 100; var h = 40; var s = 0; if (doCalculate) s = w * h; var width = 100, height = 40, area = 0; if (doCalculate) { area = width * height; } 1. Follow Ext JS coding patterns
  • 39. 2. Design classes for configurability ?Why? ! It will allow your class to be easily configured without the use of huge overrides. This concept is seen throughout all of Ext JS.
  • 40. MyTip = Ext.extend(Ext.Tooltip, { onMouseLeave: function(){ this.el.fadeOut(200); } } 2. Design classes for configurability MyTip = Ext.extend(Ext.Tooltip, { fadeDuration: 200, onMouseLeave : function(){ this.el.fadeOut(this.fadeDuration); } }
  • 41. 3. Make key functionality easily overridable ?Why? ! It will allow your class to be easily altered without the use of huge overrides. This concept is seen throughout all of Ext JS.
  • 42. initComponent : function(){ this.tpl = new Ext.XTemplate( ”<div>{foo}</div>” ); // .... } 3. Make key functionality easily overridable initComponent : function(){ if (!this.tpl) { this.tpl = new Ext.XTemplate( '<div>{foo}</div>” ); } // .... }
  • 43. 4. Make classes localizable ?Why? ! Because you know your boss will ask about localization support at some point.
  • 44. MyClass = Ext.extend(Ext.Toolbar, { constructor: function() { this.add({ text : 'No data to display’ }); .... }); 4. Make classes localizable MyClass = Ext.extend(Ext.Toolbar, { noDataText : 'No data to display’, constructor: function() { this.add({ text : this.noDataText }); }); });
  • 45. 5. Use a syntax checker ?Why? ! Helps you find global variable leaks, extra commas etc. Use JsLint or JavaScriptLint (beware, JsLint WILL hurt your feelings).
  • 46. ?Why? ! Because noone likes memory leaks. Override the onDestroy method to clean up any additional elements, event listeners etc... 6. Clean up after yourself
  • 47. MyPanel = Ext.extend(Ext.Panel, { constructor: function() { this.someEl = new Ext.Element(); }, .... }); MyPanel = Ext.extend(Ext.Panel, { constructor: function() { this.someEl = new Ext.Element(); }, onDestroy: function() { this.someEl.destroy(); // Call superclass destroy method... } }); 6. Clean up after yourself
  • 48. ?Why? ! Because you (or someone else) may want to make use of the lazy instantiation mechanism provided by Ext. 7. Define an xtype
  • 49. MyPanel = Ext.extend(Ext.Panel, { constructor: function() { // ... } }); MyPanel = Ext.extend(Ext.Panel, { constructor: function() { // ... } }); Ext.reg(’mypanel’, MyPanel); 7. Define an xtype
  • 50. ?Why? ! Because other developers will likely read your code. By using the JSDoc syntax you can generate beautiful documentation looking like the Ext online API (using Ext-Doc). 8. Document your extension
  • 51. MyClass = Ext.extend(Ext.Panel, { // ... }); /** * @class MyClass * @extends Ext.Panel * @constructor * @param {Object} config The cfg object */ MyClass = Ext.extend(Ext.Panel, { // ... }); 8. Document your extension
  • 52. ?Why? !Noone likes bugs. Some examples: * What happens if you include multiple instances of your extension? * What happens when it’s destroyed? Any leaked DOM nodes etc? 9. Test edge cases
  • 53. ?Why? ! You might not care, but everyone that wants to use your extension in a production environment will (should) care. 10. Include a license
  • 54. Additional resources • Sencha Learning Center: http://www.sencha.com/learn/Ext_2_Overview • Saki’s Blog: http://blog.extjs.eu/know-how/extension-or- plugin/