extjs中的Config及Mixins概念(转)

Extjs 4中,为类型系统引入了Config概念,Config就是配置项的意思,用{configItem1:value1...}表示;

在对象构造的时候,会调用this.initConfig(config)将配置项初始化,每个配置项自动生成4个函数:get set reset apply。

  1. Ext.define('My.own.Window', {
  2. /** @readonly */
  3. isWindow: true,
  4. config: {
  5. title: 'Title Here',
  6. bottomBar: {
  7. enabled: true,
  8. height: 50,
  9. resizable: false
  10. }
  11. },
  12. constructor: function(config) {
  13. this.initConfig(config);
  14. return this;
  15. },
  16. applyTitle: function(title) {
  17. if (!Ext.isString(title) || title.length === 0) {
  18. alert('Error: Title must be a valid non-empty string');
  19. }
  20. else {
  21. return title;
  22. }
  23. },
  24. applyBottomBar: function(bottomBar) {
  25. if (bottomBar && bottomBar.enabled) {
  26. if (!this.bottomBar) {
  27. return Ext.create('My.own.WindowBottomBar', bottomBar);
  28. }
  29. else {
  30. this.bottomBar.setConfig(bottomBar);
  31. }
  32. }
  33. }
  34. });

如何使用:

 
 

  1. var myWindow = Ext.create('My.own.Window', {
  2. title: 'Hello World',
  3. bottomBar: {
  4. height: 60
  5. }
  6. });
  7. alert(myWindow.getTitle()); // alerts "Hello World"
  8. myWindow.setTitle('Something New');
  9. alert(myWindow.getTitle()); // alerts "Something New"
  10. myWindow.setTitle(null); // alerts "Error: Title must be a valid non-empty string"
  11. 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?

  1. Ext.define('Computer', {
  2. statics: {
  3. instanceCount: 0,
  4. factory: function(brand) {
  5. // 'this' in static methods refer to the class itself
  6. return new this({brand: brand});
  7. }
  8. },
  9. config: {
  10. brand: null
  11. },
  12. constructor: function(config) {
  13. this.initConfig(config);
  14. // the 'self' property of an instance refers to its class
  15. this.self.instanceCount ++;
  16. return this;
  17. }
  18. });
  19. var dellComputer = Computer.factory('Dell');
  20. var appleComputer = Computer.factory('Mac');
  21. alert(appleComputer.getBrand()); // using the auto-generated getter to get the value of a config property. Alerts "Mac"
  22. 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"

pv.aspx?id=24

新概念,相当于调用Ext.apply(this,other)将other类中的方法合并到当前的类中,也相当于另一种形式的继承。

下面用代码测试一下,使用了Siesta测试框架,有兴趣可以google一下,很强大的测试系统。

Js代码

  1. StartTest(function(t) {
  2. t.diag("Extjs common test");
  3. t.ok(Ext,"Ext is here");
  4. Ext.define("test.Talk",
  5. {
  6. talk:function()
  7. {
  8. return 'talk'
  9. }
  10. }
  11. );
  12. Ext.define("test.Person",
  13. {
  14. mixins:
  15. {
  16. everyOneNeedTalk:"test.Talk"
  17. }
  18. });
  19. var p = Ext.create("test.Person");
  20. t.is('talk',p.talk(),'The method is mixin')
  21. Ext.define("test.Student",{
  22. config:{
  23. gender:'boy'
  24. },
  25. constructor:function(config){
  26. this.initConfig(config);
  27. //这里需要调用initConfig,否则不会自动生成getter 和 setter
  28. }
  29. });
  30. var s = Ext.create('test.Student')
  31. t.is(s.getGender(),'boy','generate getter')
  32. s.setGender('girl');
  33. t.is(s.getGender(),'girl','generate setter')
  34. t.done(); // Optional, marks the correct exit point from the test
  35. });

转载于:https://www.cnblogs.com/dwfbenben/archive/2012/03/22/2412505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值