定义类的方法:
/**
* Application Layout
* by Jozef Sakalos, aka Saki
* http://extjs.com/learn/Tutorial:Application_Layout_for_Beginners
*/
// reference local blank image
Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
// create namespace
Ext.namespace('myNameSpace');
// Just to allow this tutorial to work for 1.1 and 2.
Ext.Ext2 = (Ext.version && (Ext.version.indexOf("2") == 0));
// create application
myNameSpace.app = function() {
// do NOT access DOM from here; elements don't exist yet
// private variables
var btn1;
var privVar1 = 11;
// private functions
var btn1Handler = function(button, event) {
alert('privVar1=' + privVar1);
alert('this.btn1Text=' + this.btn1Text);
};
// public space
return {
// public properties, e.g. strings to translate
btn1Text: 'Button 1',
// public methods
init: function() {
if (Ext.Ext2) {
btn1 = new Ext.Button({
renderTo: 'btn1-ct',
text: this.btn1Text,
handler: btn1Handler
});
} else {
btn1 = new Ext.Button('btn1-ct', {
text: this.btn1Text,
handler: btn1Handler
});
}
}
};
}(); // end of app
// end of file
注意私有变量的位置;
公有成员定义在RETURN里;
公有成员不加任何修饰直接访问私有变量,但访问同级的公有成员要加this.。
扩展EXT类的方法:
// 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);
};
// MyPanel Extends Ext.Panel
Ext.extend(MyPanel, Ext.Panel, {
// Here you can add static variables for the class. variables that will have
// the same value for all object instances of this class.
// If you are not sure put it in the constructor above. Dont put any abject
// created with 'new' or 'xtype' here. You are safer putting it in the config
// option in the constructor.
// 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'
});
var mysecondpanel = new MyPanel({
title: 'My Second Panel'
});
先定义构造函数,定制CONFIG,再EXTEND类,覆盖定义原类的函数。
posted on 2009-03-03 15:57
我爱佳娃 阅读(1064)
评论(1) 编辑 收藏 所属分类:
AJAX