zoukankan      html  css  js  c++  java
  • 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"

    新概念,相当于调用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. });
  • 相关阅读:
    避免前置声明
    CLion在WSL上远程调试代码设置
    push_back与构造函数
    _BLOCK_TYPE_IS_VALID(pHead->nBlockUse问题解析
    Qt报错
    关于引用与指针实现多态的一些记录
    Vue-Axios异步通信
    Kafka概述
    学习Ajax看着一篇就够了
    学习Json看着一篇就够了
  • 原文地址:https://www.cnblogs.com/dwfbenben/p/2412505.html
Copyright © 2011-2022 走看看