extjs classes

9
ExtJS Classes By Aaron Conran

Upload: lara-morse

Post on 31-Dec-2015

21 views

Category:

Documents


0 download

DESCRIPTION

ExtJS Classes. By Aaron Conran. Creating Classes. Creating classes in JavaScript is easy as creating a constructor function and using the new keyword when creating an instance of that class. Person Class: var Person = function(config) { Ext.apply(this, config); };. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ExtJS Classes

ExtJSClasses

By Aaron Conran

Page 2: ExtJS Classes

Creating Classes

• Creating classes in JavaScript is easy as creating a constructor function and using the new keyword when creating an instance of that class.

Person Class:

var Person = function(config) { Ext.apply(this, config);

};

Using the Person class:

var me = new Person({fName: ‘Aaron’,lName: ‘Conran’, dob: ’03/23/1984’});

Page 3: ExtJS Classes

Ext.apply

• Ext.apply copies all attributes of one object to another.• Ext.apply is often used at the beginning of constructors

to copy configuration arguments to the this scope.• The new keyword creates a new blank object in the

scope of this.• You can also supply a 3rd argument as a default

configuration.

Ex:

Ext.apply(this, config);

// with defaults

var defConfig = {test: ‘abc’};

Ext.apply(this, config, defConfig);

Page 4: ExtJS Classes

Ext.applyIf

• Ext.applyIf works similarly to Ext.apply except if properties already exist they won’t be overwritten.

• Ex:var point = point || {};

var default = {x: 0, y: 0};

Ext.applyIf(point, default);

Page 5: ExtJS Classes

Ext.extend

• Ext.extend is used to extend or inherit from classes which already exist.

• Generic Pattern:var SubClass = function() { SubClass.superclass.constructor.call(this);};Ext.extend(SubClass, BaseClass, { newMethod : function() {}, overriddenMethod : function() {}};

• SubClass extends BaseClass and overrides overridenMethod and adds newMethod.

Page 6: ExtJS Classes

superclass.constructor

• The superclass.constructor property points to our base (or super) class constructor.

• We use the JavaScript call method to run the constructor in the scope of this.

• this will always be our first argument of call. Other arguments will be passed to our base constructor:

• Ex:BaseClass.superclass.constructor.call(this, config);

Page 7: ExtJS Classes

Extending an Ext Class

• Extending and Customizing Ext classes is easy

• Goal: Create a extension of BasicDialog– New class DefaultDialog which extends from

BasicDialog– Provide a set of defaults for dialogs

• modal, width, height, shadow, draggable, etc

– No need to add/override methods to BasicDialog

Page 8: ExtJS Classes

Extending an Ext class var DefaultDialog = function(config) {

var config = config || {}; // default config to blank object var defConfig = {title: 'Default', // provide a default config

height: 130, width: 250, shadow: true, modal: true, draggable:true, fixedcenter:true, collapsible: false, closable: true, resizable:false};

Ext.applyIf(config, defConfig); // apply defConfig IF config does not have propertyvar el = Ext.DomHelper.append(document.body, {tag: 'div'}); // create elDefaultDialog.superclass.constructor.call(this, el, config); // run superclass

}; Ext.extend(DefaultDialog, Ext.BasicDialog); // DefaultDialog extends Ext.BasicDialog

Page 9: ExtJS Classes

DefaultDialog example

• We can now re-use the DefaultDialog class • By passing configuration options we can override the

defaults• By omitting the configuration, we assume the defaults

dlg = new DefaultDialog({title: 'First Dialog', width: 300});

dlg.show();

dlg2 = new DefaultDialog();

dlg2.show();