Extjs 4中,为类型系统引入了Config概念,Config就是配置项的意思,用{configItem1:value1...}表示;
在对象构造的时候,会调用this.initConfig(config)将配置项初始化,每个配置项自动生成4个函数:get set reset apply。
- 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
- 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
3. 静态
使用statics定义静态成员
[javascript] view plaincopyprint?
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.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.apply(this,other)将other类中的方法合并到当前的类中,也相当于另一种形式的继承。
下面用代码测试一下,使用了Siesta测试框架,有兴趣可以google一下,很强大的测试系统。
Js代码
- StartTest(function(t) {
- t.diag("Extjs common test");
- t.ok(Ext,"Ext is here");
- Ext.define("test.Talk",
- {
- talk:function()
- {
- return 'talk'
- }
- }
- );
- Ext.define("test.Person",
- {
- mixins:
- {
- everyOneNeedTalk:"test.Talk"
- }
- });
- var p = Ext.create("test.Person");
- t.is('talk',p.talk(),'The method is mixin')
- Ext.define("test.Student",{
- config:{
- gender:'boy'
- },
- constructor:function(config){
- this.initConfig(config);
- //这里需要调用initConfig,否则不会自动生成getter 和 setter
- }
- });
- var s = Ext.create('test.Student')
- t.is(s.getGender(),'boy','generate getter')
- s.setGender('girl');
- t.is(s.getGender(),'girl','generate setter')
- t.done(); // Optional, marks the correct exit point from the test
- });