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

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中利用Ajax数据源来绘制线形图表(Line chart)。ExtJS是一个强大的JavaScript库,专为构建富客户端应用程序而设计,它提供了丰富的图表组件,使得数据可视化变得简单易行。让...

    ExtJS 常用组件样式修改

    通过以上方法,开发者可以有效地自定义ExtJS中的工具栏和菜单样式,从而提升应用程序的用户体验和视觉效果。此外,还可以进一步探索其他组件如树形菜单、表格和按钮等的样式修改方法,使整个应用界面更加一致和美观...

    extjs中文文档大全

    1. **ExtJS实用开发指南**:此部分主要针对ExtJS的实际应用,包含如何快速上手、组件的使用、布局管理、数据绑定和Ajax交互等内容。开发者可以从中了解到如何创建基本的页面结构,使用GridPanel展示数据,以及通过...

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

    在"ExtJs3.3中文API.CHM"中,开发者可以找到关于ExtJS 3.3的所有API和类的详细描述,以及相关的示例代码。 ExtJS 3.3的核心特性包括: 1. **组件化**:ExtJS基于组件模型,提供了一系列预定义的UI组件,如按钮、...

    extjs中文帮助文档和英文api

    这份“extjs中文帮助文档和英文api”提供了全面的ExtJS API信息,帮助开发者理解并有效地使用这个框架。 中文帮助文档(extjs帮助文档.chm)对ExtJS的各种组件、类、方法、属性进行了详尽的解释,使得不懂英文或者...

    Extjs中文文档

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

    Extjs 中文API文档

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

    Extjs中文文档.pdf

    表格控件是ExtJS中极为重要的一部分,提供了大量的特性来增强用户体验和功能性。 **核心功能**: - **基本功能**:支持单选或多选行、高亮显示选中行、列宽调整、按列排序等。 - **高级功能**:自动生成行号、全选...

    ExtJs官方网站中文的入门指南 javascript

    **ExtJs**是一款基于JavaScript的企业级Web应用开发框架,它提供了丰富的UI组件和强大的功能,帮助开发者快速构建高性能的Web应用程序。本文档旨在引导初学者快速入门ExtJs,通过实例演示如何搭建开发环境并实现简单...

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

    在《ExtJs 实例》中,你将找到各种实际应用场景的代码示例,这些实例涵盖了ExtJs的各种组件和功能。通过学习这些实例,你可以更好地理解如何在实际项目中应用ExtJs,例如如何创建网格(Grid)、表单(Form)、树形...

    中文的Extjs的api手册

    手册中涵盖了ExtJS 3.3的所有类、方法、事件、属性和配置选项,对于开发者来说是不可或缺的工具。 1. **组件系统**:ExtJS的核心在于其组件模型,包括窗口(Window)、面板(Panel)、表格(Grid)、表单(Form)等...

    轻松搞定Extjs 带目录

    - **命名空间**: 命名空间在Extjs中用于组织和管理代码,避免变量和函数名的冲突。 #### 3. Extjs OOP Extjs支持面向对象编程,其对象模型包括: - 类的定义(Ext.extend) - 对象实例化(通过new关键字) - 原型...

    extJs3升级extjs4方案

    ExtJS3 升级到 ExtJS4 方案 ExtJS3 升级到 ExtJS4 需要修改大量代码,主要是因为 ExtJS4 配备了一类新的系统,不向后兼容...ExtJS3 升级到 ExtJS4 需要修改大量代码,需要我们重新学习和适应 ExtJS4 的新特性和变化。

    ExtJs中文API

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

    EXTJS 多文件上传

    9. **文件管理**:在上传成功后,EXTJS 还可以帮助管理和展示已上传的文件,例如在列表中显示文件名、大小、日期等信息。 `MultiFileUploadField.js` 文件是EXTJS框架中实现`MultiFileUploadField` 功能的具体代码...

    ExtJS3.0中文API

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

    extjs四个例子,一个中文API

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

    ExtJS中文手册.doc

    关于文件名称列表中的"ExtJS中文手册.doc",这表明手册以Microsoft Word文档格式提供,方便开发者打印或离线阅读。可能包含以下章节: 1. **基础概念**:介绍ExtJS的基本结构,如类系统、MVC模式、组件生命周期等。...

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

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

Global site tag (gtag.js) - Google Analytics