Professional Documents
Culture Documents
Javascript Object
10
20
Foo
Object.protoype
constructor
Object
function Mammal(name){
this.name=name;
this.offspring=[];
}
Mammal.prototype.baby=fun
ction(){
var newBaby=new
Mammal("Baby
"+this.name);
this.offspring.push(newBab
y);
return newBaby;
}
Inheritance Summary
You cause a class to inherit using
ChildClassName.prototype = new ParentClass();
You need to remember to reset the constructor
property for the class using
ChildClassName.prototype.constructor=ChildClas
sName
You can call ancestor class methods which your
child class has overridden using the
Function.call() method.
Inheritance: ExtJS
Ext provides a utility function called Ext.extend that is
the mechanism for implementing class inheritance using
the Ext framework.
It gives you the ability to modify or extend the base
functionality of any JavaScript class without making code
changes directly to the class itself .
It is the preferred method for extending Ext components.
MyClass = Ext.extend(Ext.SomeClass, {
someFunction : function(arg1, arg2){
// custom code
// call base class
MyClass.superclass.someFunction.call(this, arg1,
arg2);
// custom code
}
);
Constructor Model
// MyPanel Extends Ext.Panel
MyPanel = Ext.extend(Ext.Panel, {
// constructor function
constructor: function(config) {
Ext.apply(this, {
// Put your pre-configured config options here
width: 300,
height: 300
});
MyPanel.superclass.constructor.apply(this, arguments);
}
});
var myfirstpanel = new MyPanel({
title: 'My First Panel'
});
Factory Pattern
function createMyPanel(config) {
return new Ext.Panel(Ext.apply({
//Pre-configured config options go here
width: 300,
height: 300
}, config));
};
var myfirstpanel = createMyPanel({
title: 'My First Panel'
});
Extending Functionality
// Constructor
var MyPanel = function(config) {
//Reusable config options here
Ext.apply(this, {
width: 300,
height: 300
});
// And Call the superclass to preserve baseclass
functionality
MyPanel.superclass.constructor.apply(this, arguments);
// Here you can add functionality that requires the object
to exist, like event handling.
this.on('click', function() {alert("You Clicked " +
this.title);}, this);
};
initComponent (new)
To extend an Ext class we do not need to create a
constructor function. We just need to assign the
return value of Ext.extend call to a variable in our
name space. Ext.extend takes the original class and a
config object as arguments and returns our extension.
All tasks that were done in a custom constructor
function are now done in initComponent function
that we almost always override. initComponent is
called early from the parent constructor function.
However, initComponent of the original class contains
useful code that needs to be executed.
Registering an xtype for your extension is not
mandatory but it is very good practice.
initComponent: function() {
//Reusable config options here
Ext.apply(this, {
width: 300,
height: 300
});
// And Call the superclass to preserve baseclass
functionality
MyPanel.superclass.initComponent.apply(this,
arguments);
// Here you can add functionality that requires the object
to
// exist, like event handling.
this.on('click',function() {alert("You Clicked " +
this.title);},this);
}
},
// New function added
myNewFunction: function() {
},
// Override an existing function
onRender: function() {
MyPanel.superclass.onRender.apply(this, arguments);
this.myNewFunction();
}
});
var myfirstpanel = new MyPanel({
title: 'My First Panel'
});
Composition or Extension
When creating a new class, the decision must be
made whether to own an instance of a utility
class which is to perform a major role, or to
extend that class.
It is recommended that you extend the nearest
base class to the functionality required.
onRender
afterRender
onAdded
onRemoved
onShow
onHide
onDisable
onEnable
onDestroy
Panel
If the required UI control must have a header, footer, or toolbars,
then Ext.Panel is the appropriate class to extend.
onCollapse
onExpand
Events in Ext
Event is a message, a function call, generated by
one (part of) program, the event source, that
notifies another (part of) program, the event
listener, that something happened.
Events in Ext
DOM Events
JavaScript Events
var myPanel = new Ext.Panel({...});
myPanel.on('some_event', function() {});
Event source in ExtJS are extension of
Ext.Util.Observable class
Custom Events
MyPanel = Ext.extend(Ext.Panel, {
initComponent:function() {
...
MyPanel.superclass.initComponent.apply(this, arguments);
...
// add custom events
this.addEvents('loaded', 'completed');
},
load:function(cfg) {
....
// fire loaded event
this.fireEvent('loaded', this, cfg);
},
complete:function(cfg) {
...
// fire completed event
this.fireEvent('completed', this, cfg);
}
});
Relay Events
Events: Summary
event is a message sent (fired) by an event
source to inform listeners that something
happened
event source is an object that can fire events
event listener is a function that is called when
event source fires an event
to listen to events we use on function to install an
event listener
to create an event source we extend Observable
class, addEvents and fireEvent
Namespaces
It is typical to include many libraries, widgets and
snippets of code from many different sources.
not a safe assumption that you have the entire
global namespace at your disposal.
When developing your own scripts you should
place all of your classes and singletons into
namespaces to avoid collisions with other
developers code.
An abstract container to hold all of your classes
and singletons
Namespaces (ExtJS)
if (!App) App = {};
if (!App.form) App.form = {};
if (!App.data) App.data = {};
Ext provides the Ext.namespace method which will setup namespaces for you,
including checking if the namespace already exists.
Debugging ExtJS
ExtJS Performance
Build your own Extjs - this will just use the
components you need, and as well css. Reduces
file size.
Compress using JSmin.
Concatenate all your Javascript sources into one
file for live running.
Serve javascript files gzipped.
Avoid memory leaks like using mon instead of on
to bind events.
Destroy any component added to this on destroy
of this.