`
adamed
  • 浏览: 181474 次
社区版块
存档分类
最新评论

ExtJS中的onRender和render

阅读更多
ExtJS老大在官方论坛的一个回复,转到这里收藏下

The split onRender/render and their siblings are the result of a deep inheritance chain and a desire to not duplicate code. They are based on a common OO design pattern called Template Method.

引用
Quote:
The template method is used to:

* let subclasses implement behaviour that can vary
* avoid duplication in the code: you look for the general code in the algorithm, and implement the variants in the subclasses
* to control at what point(s) subclassing is allowed.


The control structure (inversion of control) that is the result of the application of a template pattern is often referred to as the Hollywood Principle: "Don't call us, we'll call you." Using this principle, the template method in a parent class controls the overall process by calling subclass methods as required. 
Let's me paint a simple picture.

var Employee = function(){
    ....
    this.addEvents({
        beforequit: true,
        quit: true
    });
}

Ext.extend(Employee, Ext.util.Observable, {
    quit : function(){
         if(this.fireEvent('beforequit', this) !== false){
             // "important logic" that must run when
             // when an employee quits
             this.fireEvent('quit', this);
         }
         return this;
    }
}

Now looking at this code, it seems very straight forward and it can't fail right? However, what happens when you add a subclass?

var Manager = Ext.extend(Employee, {
     quit : function(){
           // Option 1 is to call the base class first
           Manager.superclass.quit.call(this);
           // then do custom Manager quit logic  
           ... Manager logic ...
           
           // Option 2 is to do the Manager specific logic first 
           ... Manager logic ...
           // and then call the base class
           Manager.superclass.quit.call(this);

           return this;
     }
});

Look at the result of the 2 options:

Option 1 has a few problems.
a) There's no way to know if the event was cancelled, so you can't stop the manager logic. You could return false if it is cancelled but then you have different return types and the API suffers.
b) The most serious issue is the "quit" event gets fired prematurely. While in this example that might be a problem, but in a Component that involves rendering and you need to know that the component is fully rendered, it would be a major issue.

Option 2 has a few problems as well:
a) The beforequit event no longer fires in advance and can no longer allow for cancellation of the action. This is a major problem.
b) The manager logic has to come first - this is a major problem especially when rendering a component and your based class is probably rendering something the subclass needs (e.g. this.el!)

Using a Template Method, we can solve this problem easily:

var Employee = function(){
    ....
    this.addEvents({
        beforequit: true,
        quit: true
    });
}

Ext.extend(Employee, Ext.util.Observable, {
    quit : function(){
         if(this.fireEvent('beforequit', this) !== false){
             this.onQuit();
             this.fireEvent('quit', this);
         }
         return this;
    },

    onQuit : function(){
         // "important logic" that must run when
         // when an employee quits
    }
    
}

var Manager = Ext.extend(Employee, {
     onQuit : function(){
           Manager.superclass.onQuit.call(this);
           .. Manager logic ...
     }
});

So not only does this solve the problem described above easily, it also means all subclasses no longer have to worry about wrapping things in events since the base class will handle that automatically.

Another even more important benefit of "Template Method" that is not present in this simple example but that is used in Component rendering is that then you can have a defined execution flow in a base class and you know all subclasses will be complete with what they need to do at each step before continuing to the next step.

Regards,
Jack
分享到:
评论

相关推荐

    ExtJs4 line chart render by ajax

    Extjs4 中Line chart使用Ajax数据源绘图,具体请参考文章http://blog.csdn.net/cl11404303/archive/2011/05/31/6457951.aspx

    ExtJS 常用组件样式修改

    这里主要介绍了ExtJS常用组件的样式修改,包括工具栏样式修改、菜单样式修改、树形菜单样式修改、表格样式修改、按钮样式修改等。

    Extjs中文文档

    Extjs中文文档,包含Extjs的基本语法和各个类的详细说明以及用法。

    Extjs 中文API文档

    Extjs 中文API文档,有对Extjs相关API的中文说明

    ExtJs 实例+ExtJs中文教程(学习extjs必备)

    ExtJs 实例+ExtJs中文教程 大量ExtJs的实例和目前最好的ExtJs中文教程,学习ExtJs的朋友可以参考一下

    extjs中文api

    extjs中文api,提供实例extjs中文api,提供实例extjs中文api,提供实例extjs中文api,提供实例extjs中文api,提供实例extjs中文api,提供实例

    ExtJs中文API

    ExtJs中文API,比较全面!并且提供了很多的例子!! ExtJs中文API,比较全面!并且提供了很多的例子!! ExtJs中文API,比较全面!并且提供了很多的例子!! ExtJs中文API,比较全面!并且提供了很多的例子!!

    Extjs 中文文档 很详细

    ExtJS中文文档,很好,很强大!绝对值得一看哦!

    extjs中文帮助文档和英文api

    全面的extjs api 使用的中文帮助文档 二合一 实用 本人正在用 很不错

    ExtJs3.3中文API.CHM_extjs3.3中文文档_

    extjs3.3的中文文档

    ExtJS3.0中文API

    绝对的ExtJS3.0中文API,本人在网上找了好久才找到的,在此奉献给大家!!解压后有30多兆!!

    extjs中本地照片预览、blob数据在oracle中存取

    此压缩包中完全能实现的功能是在extjs中让本地照片预览,并且将地址传递给java后台,将图片文件以blob的形式存储到oracle数据库,并且可以默认将数据库中的数据第一次加载在预览框里(也就是从数据库中读出blob数据...

    EXTJS日期扩展年月和年月日时分秒

    只有年月选择--dateYM.html (这个在EXTJS4.0和4.1下显示都是可以的) 2.扩展日期选择时分秒--dateTimeField4.0.html (这个只在EXTJS4.0下显示可以) EXTJS4.1文件夹下面包括 1.只有年月选择--dateYM.html ...

    extjs四个例子,一个中文API

    新手学习,初学extjs时用刚刚好,里边有三个例子,还有一个我自己写的例子(简单粗暴动态菜单),还有一个中文API

    extjs 中文 api chm格式的

    extjs 中文 api chm格式的!可以跟好的帮助你学习富客户端技术!!!!!!

    ExtJS中文资料、参考文档

    收集了ExtJS中文站上的资料,共52篇文章,保留了文章的原始风貌,支持关键字查询,里面的内容可以选择后ctrl-C拷出来。赚个辛苦分。

    extjs4 中文API

    extjs4.1 中文API

    extjs4中文视频下载地址

    第一讲:extjs4.0概述和HELLO WORD程序 第二讲:extjs4.0的新特性 第三讲:extjs4.0数据模型--Model 第四讲:extjs4.0的数据代理-Proxy 第五讲:extjs4.0的读写器reader,writer 第六讲:extjs4.0的数据集store 第七...

    Extjs中文文档.pdf

    Extjs 中文文档 学习文档

    extjs中使用FusionChart举例

    extjs中使用FusionChart举例

Global site tag (gtag.js) - Google Analytics