在其历史上的第一次,Ext JS的经历了一次巨大的重构。几乎每一个类都被重写,因此,在开始写代码之前很好的理解各类是很重要的。
本手册适用于任何的开发者,它分为4个部分:
Ext JS 4有300多个类。有一个超过20万来自世界各地开发者的用户社区。在同一个框架下,我们面对着一荐巨大的挑战(共同的代码架构)
对类,命名空间,文件名使用统一的命名命名约定,有利于对代码的组织,结构化和更有可读性
类名可能只包含字母数字。数字是可以,但大多情况下不鼓励使用,除非是技术术语,同样的不要使用下划线,连接线和非字母数字字符。例如:
MyCompany.useful_util.Debug_Toolbar 不鼓励
MyCompany.util.Base64
可以MyCompany.data.CoolProxy MyCompany.Application
顶级的命名空间和实际类名应在驼峰,一切应全部小写。例如:
MyCompany.form.action.AutoLoad
一个类一个文件,类名和文件名相同。例如:
Ext.util.Observable
is stored inpath/to/src/Ext/util/Observable.js
Ext.form.action.Submit
is stored inpath/to/src/Ext/form/action/Submit.js
MyCompany.chart.axis.Numeric
is stored inpath/to/src/MyCompany/chart/axis/Numeric.js
Ext.MessageBox.YES= "Yes"
Ext.MessageBox.NO= "No"
MyCompany.alien.Math.PI = "4.13"
If you have ever used any previous version of Ext JS, you are certainly familiar withExt.extend
to create a class:
var MyWindow = Ext.extend(Object, { ... });
我们先看个例子:
My.cool.Window = Ext.extend(Ext.Window, { ... });
在这个例子中,我们命名了继承自Ext.Window的新类。我们需要解决两个问题:
Ext.ns('My.cool'); My.cool.Window = Ext.extend(Ext.Window, { ... });
Ext JS 4 提供了Ext.define
方式创建类.例如:
Ext.define(className, members, onClassCreated);
className
: 类名 members
:键 - 值对的集合 onClassCreated
:类创建好后的回调函数 Example:
注意我们使用Ext.create()创建了一个新实例。我们也可以用new关键字(newMy.sample.Person())。然而,这是不建议的,因为使用Ext.create()可以让你动态加载所依赖的类Ext.define('My.sample.Person', { name: 'Unknown', constructor: function(name) { if (name) { this.name = name; } return this; }, eat: function(foodType) { alert(this.name + " is eating: " + foodType); return this; } }); var aaron = Ext.create('My.sample.Person', 'Aaron'); aaron.eat("Salad"); // alert("Aaron is eating: Salad");
在Ext JS 4中我们声明了config属性,在创建实例前由EXt.Class处理
下面是一个怎么使用的例子Ext.define('My.own.Window', { /** @readonly */ isWindow: true, config: { title: 'Title Here', bottomBar: { enabled: true, height: 50, resizable: false } }, constructor: function(config) { this.initConfig(config); return this; }, applyTitle: function(title) { if (!Ext.isString(title) || title.length === 0) { alert('Error: Title must be a valid non-empty string'); } else { return title; } }, applyBottomBar: function(bottomBar) { if (bottomBar && bottomBar.enabled) { if (!this.bottomBar) { return Ext.create('My.own.WindowBottomBar', bottomBar); } else { this.bottomBar.setConfig(bottomBar); } } } });
var myWindow = Ext.create('My.own.Window', { title: 'Hello World', bottomBar: { height: 60 } }); alert(myWindow.getTitle()); // alerts "Hello World" myWindow.setTitle('Something New'); alert(myWindow.getTitle()); // alerts "Something New" myWindow.setTitle(null); // alerts "Error: Title must be a valid non-empty string" myWindow.setBottomBar({ height: 100 }); // Bottom bar's height is changed to 100
使用statics定义静态成员
Ext.define('Computer', { statics: { instanceCount: 0, factory: function(brand) { // 'this' in static methods refer to the class itself return new this({brand: brand}); } }, config: { brand: null }, constructor: function(config) { this.initConfig(config); // the 'self' property of an instance refers to its class this.self.instanceCount ++; return this; } }); var dellComputer = Computer.factory('Dell'); var appleComputer = Computer.factory('Mac'); alert(appleComputer.getBrand()); // using the auto-generated getter to get the value of a config property. Alerts "Mac" alert(Computer.instanceCount); // Alerts "2"
Ext Js 4 有一些有用的功能可以帮助你更好的调式问题和对错误的处理
您可以使用Ext.getDisplayName()来获得的任何方法名称。可以抛出带有类名和方法名错误描述,这是特别有用:
throw new Error('['+ Ext.getDisplayName(arguments.callee) +'] Some message here');
在任何使用Ext.define()定义的任何类方法抛出错误时,你应该看到调用堆栈中的方法和类的名称,如果你正在使用一个基于WebKit的浏览器(Chrome或Safari)。例如,在Chrome是什么样子: