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

如何使用ExtJS构建应用系统2

阅读更多
原文地址:http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/

Writing a Big Application in Ext (Part 2)
2. February 2009 – 14:12 Important
If you have not already done so, study Writing a Big Application in Ext (Part 1) before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first part.

Introduction
It has been almost one year since I have published the first part of this article. I have been successfully using the concept of pre-configured classes personally to write a really big application (~150 javascript files, ~60,000 lines of code, plus server-side and css). The application is fully modularized, each class in separate file and it has proven that it can be easily developed, managed and debugged.

Unfortunately, the same has not been always true for other users, they were hitting various problems and Ext Support Team have had to handle may questions and help requests. These accumulated to the degree where the concept has been called “absolute abomination” (read absolutely hated) and it was stated that “it causes problems”.

Note: As “fast cars” do not cause accidents but “slow drivers driving fast cars” do, the concept itself cannot be a cause of the problems but its application can.

In any case, there must be some illogic if I can use the concept but others cannot.

Thus, I have looked deeper in it and I have isolated some pitfalls the users can run into on the course of development. I will also write on “dos” and “don’ts” and on when to use a pre-configured class and when not. I will not compare this concept to another application design concepts (factory functions, in-line configuration, on-demand load, or other) because 1) I do not use them and 2) I do not want to turn this article into a Linux versus Windows discussion.

It is fully up to you which application design concept you choose. However, if you do choose this one then follow the rules I’m going to lay down.

Most Common Sources of Troubles
•Dull Copy&Paste
•Extending a non-Ext.Component class
•Not calling parent methods
•initialConfig
•listeners
Dull Copy&Paste
Do you know such people? They post on a forum:

I need to drag from tree to qrid, gimme plz complete codez

And if somebody altruistic writes a fragment of “codez” for him in a sheer attempt to help the response is going to be:

Your codez don’t work. Help me plz my manager wants it working

Do you see what happened? A dull “developer” ordered a code on the forum, he’s got some, copied&pasted it to his application without a clue what the code does, maybe hasn’t even changed url that still points to your server and the result is: it doesn’t work.

Well, this is an extreme (but not so rare as you would think), nevertheless, copying&pasting without understanding of what the copied&pasted code does can lead only to frustrations.

I am not against Copy&Paste in general, it can save a lot of time and I also occasionally do it, but I am against not-understanding or mis-understanding not only of coding but also of life.

The Rule: Do Copy&Paste but always with full understanding of what the code does.

Extending a non-Ext.Component class
If an Ext class does not inherit from Ext.Component the initComponent is never called so the code you have written there is never executed. This is fragment from Ext.Component constructor:

     this.getId();
    Ext.ComponentMgr.register(this);
    Ext.Component.superclass.constructor.call(this);
 
    if(this.baseAction){
        this.baseAction.addComponent(this);
    }
 
    this.initComponent(); 

Ext classes that do not inherit from Ext.Component do not have this.initComponent(); line in their constructors.

The Rule: Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.

Not calling parent methods
It happens very often that you do not only add some methods in your extended class but that you modify existing ones. initComponent being the first example. onRender, afterLayout are other (but not only) frequently overriden methods.

These methods are already implemented in the class you are extending and its parents so if you forget the line:

// in initComponent override
YourExtension.superclass.initComponent.apply(this, arguments);

// or in onRender override
YourExtension.superclass.onRender.apply(this, arguments);

// or in afterLayout override
YourExtension.superclass.afterLayout.apply(this, arguments);your class will not work.

The Rule: Never forget to call the parent method, unless you exactly know what you are doing.

initialConfig
The constructor of Ext.Component saves the config passed to it as initialConfig:


     /**
     * This Component's initial configuration specification. Read-only.
     * @type Object
     * @property initialConfig
     */
    this.initialConfig = config;
 
    Ext.apply(this, config);
    this.addEvents(/* abbreviated for clarity */);
    this.getId();
    Ext.ComponentMgr.register(this);
    Ext.Component.superclass.constructor.call(this);
 
    if(this.baseAction){
        this.baseAction.addComponent(this);
    }
 
    this.initComponent(); 

You see what happens? The constructor saves initialConfig before initComponent is executed. Thus, all configuration you write in initComponent is not saved. I have overlooked this in first versions of my templates and examples mainly because there is only a couple of classes that refer to initialConfig and even in these classes the absence of properly saved initialConfig causes problems very rarely. These Ext classes refer to initialConfig:

•AnchorLayout
•BorderLayout
•Action
•GridPanel
•Tip
•Combo
•Form
Now, I have updated all my examples, extensions, templates and main site to include this “magic” pattern:

 // create config
var config = {
    // put your config here
}; // eo config object
 
// apply config
Ext.apply(this, Ext.apply(this.initialConfig, config));
 
// call parent

YourExtension.superclass.initComponent.apply(this, arguments);
The Rule: Ensure that your extension saves initialConfig.

listeners
If you try to install event handlers by setting property listeners in your config they will not work. Why? The answer lies again in the order of actions in Ext.Component constructor:

     Ext.Component.superclass.constructor.call(this);
 
    if(this.baseAction){
        this.baseAction.addComponent(this);
    }
 
    this.initComponent(); 

As you can see, the constructor calls its parent, that is Ext.util.Observable before initComponent. Ext.util.Observable constructor executes:

 Ext.util.Observable = function(){
    if(this.listeners){
        this.on(this.listeners);
        delete this.listeners;
    }
}; 

Any listeners set in initComponent are thus ignored.

There is an easy workaround. Put constructor method in your extension:

 
 constructor:function(config) {
    // parent constructor call pre-processing - configure listeners here
    config = config || {};
    config.listeners = config.listeners || {};
    Ext.applyIf(config.listeners, {
        // add listeners config here
    });
 
    // call parent constructor
    AnExtension.superclass.constructor.call(this, config);
 
    // parent constructor call post-processing
 
} // eo function constructor 

and define your listeners therein.

The Rule: Define listeners in constructor method.

Conclusion
If you decide to use my way of writing a big application then follow these rules:

1.Do Copy&Paste but always with full understanding of what the code does.
2.Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.
3.Never forget to call the parent method, unless you exactly know what you are doing.
4.Ensure that your extension saves initialConfig.
5.Define listeners in constructor method.
Happy extending!
分享到:
评论

相关推荐

    ExtJS4+S2SH+Oracle9i开发电子商业汇票系统

    本教程主要讲述如何利用Struts2,Hibernate,Spring架构来构建企业级应用解决方案,前台用ExtJS展现主要分为两部分完成 第一部分介绍电子商业汇票系统业务。 第二部分,将实际应用(某银行-电子商业汇票系统)为例,...

    信息办公ExtJS 2.2 图书管理系统-bmsh.rar

    标题:探索ExtJS 2.2 图书管理系统:一款适用于毕业与课程设计的高效源码工具在追求高效信息管理和便捷用户操作的今天,基于Web的应用程序已成为软件开发的重要分支。特别是对于在校学生而言,一个功能齐全、界面...

    上传下载ExtJS 2.2 开源网络硬盘系统-dogdisk.rar

    通过分析其源代码,学生可以深入了解如何使用JSP与ExtJS技术协同工作,提升前端与后端开发技能,并掌握构建复杂Web应用的实践经验。综上所述,Dogdisk不仅是一个功能强大的网络硬盘系统,更是一个学习和实践现代Web...

    [信息办公]ExtJS2.2图书管理系统_bmsh.zip

    项目的目标是构建一个高效、可靠、易于维护的Web应用程序。通过使用SSM或SSH框架,可以实现代码的模块化和解耦,提高开发效率和代码质量。MySQL数据库作为数据存储,可以提供稳定的数据存储和快速的数据查询。而JSP...

    [上传下载]ExtJS2.2开源网络硬盘系统_dogdisk.zip

    项目的目标是构建一个高效、可靠、易于维护的Web应用程序。通过使用SSM或SSH框架,可以实现代码的模块化和解耦,提高开发效率和代码质量。MySQL数据库作为数据存储,可以提供稳定的数据存储和快速的数据查询。而JSP...

    spring-extjs-sample

    简单的 spring mvc 应用程序,带有引导皮肤和 extjs 网格组件和嵌入式 H2 数据库。 应用程序由三页组成。 主页以可排序的方式显示所有公司的员工。 其他两个页面显示来自部门 A 和 B 的员工。服务器存储依赖于带有 ...

    基于Spring4+SpringMVC+Hibernate5的燃气进销存管理系统.zip

    ④ 熟练使用 maven 构建工具。 ⑤ 站在企业的角度,试着搭建自己的一个底层基础框架。 ⑥ 建立完整的燃气管进销存管理系统,进行测试并分析结果。 ⑦ 将系统部署到互联网上,以实现真正的 Web 应用。 系统...

    基于ExtJS和SSH的Web应用架构的研究与实现 (2010年)

    为了构建更加动态,响应更加灵敏的Web应用程序,作者对基于AJAX技术的ExtJS框架进行了深入研究,提出了一种新的基于ExtJS+SSH的Web应用架构,极大地提升了Web应用程序的响应速度和用户体验,使服务器更加方便、快捷地和...

    MySQL技术实战开发CRM客户关系管理系统

    由于 Maven 的缺省构建规则有较高的可重用性,所以常常用两三行 Maven 构建脚本就可以构建简单的项目,而使用 Ant 则需要十几行。事实上,由于 Maven 的面向项目的方法,许多 Apache Jakarta 项目发文时使用 Maven,...

    GitHub_Gist_API_Demo:使用 Ext JS 构建的演示应用程序,展示了 GitHub Gists API

    该应用程序使用 Ext JS 5.0.1 构建,并使用 Sencha Cmd (5.1.0.26) 搭建脚手架。 因此,原始项目自动生成留下了相当多的样板代码。 GitHub API 根据 Sencha 生态系统的最佳实践和普遍接受的架构,第三方...

    利用Ajax与ExtJS 改善用户体验

    从Ajax原理,ExtJS在OA型系统中的应用及性能调优出发,设计实现了三农电子商城后台管理系统,并探讨了ExtJS在基于B/S结构的OA型电子商务系统中对用户体验的改善。系统界面美观,操作方便,开发效率高,维护规范,...

    个人账户管理系统.zip

     说明:项目采用composer依赖构建,框架采用ZendFramework2,表现层使用ExtJS 4.1.x,ORM采用Propel,项目借鉴J2EE三层设计模式(应用到了Service、DAO,并通过mnapoli/php-di-zf2进行依赖注入以更好的提高可测性)...

    Log-viewer-syslog-extjs:系统日志查看 TCC

    sass 构建的支持代码(全局函数、mixin 等)日志/sass/src 此文件夹包含定义与应用程序的 javascript 代码构建中包含的类相对应的 css 规则的 sass 文件。 默认情况下,此文件夹中的文件映射到应用程序的根命名空间...

    基于SSH2+Maven+EasyUI+MySQL技术实战开发易买网电子商务交易平台

    使之提供更加高效和友好的用户接口,越来越多的企业和开发人员选择使用富客户端技术构建商业应用,本课程主要是介绍了解最流行的富客户端框架jquery - easyUI API及熟悉掌握其高级特性,并结合SSH2框架与Maven实现...

    支持桌面和移动的SPA框架RIAEasy.zip

    旨在实现RIA/SPA应用的快速、高质量开发,实现模块化开发,实现移动、桌面系统统一的跨浏览器开发。可以使用RIAStudio在线可视化设计器。 RIAEasy基于webComponent概念设计,包括一整套基础控件,具有良好的运行...

    magnificat-electron:一个使用Ext JS为Electron编写的简单音乐播放器

    壮丽的Magnificat旨在成为具有大量CD收藏的古典音乐发烧友的全功能编目应用程序。 我本打算使用Ext JS在Electron中编写它,但...): Sencha Ext JS 6.2.0 6.5.1+构建工具 6.11以上1.7.6+(通过安装)快速开始 $ git cl

    sencha-wysiwyg-editor:Sencha WYSIWYG编辑器,可让您编辑应用程序视图,源代码等-Source code editor

    选择一个框架/工具包(经典或现代)来构建您的ExtJS应用程序。 选择您的主题。 选择您的设备布局。 从基于模板的入门视图包中进行选择。 使用WYSIWYG编辑器直观地设计和创建视图: 工具箱(组件托盘): 可...

    基于MVCS模式的组件化手机Web前端的研究与应用

    本文以MVC模型为架构,ExtJS组件为基础,数据存储(Store)为数据容器,构建了MVCS (Model-View-Controller-Store)模型,并以书籍信息系统为例,说明使用该模型完成组件化手机Web前端设计的方法和实现步骤,展示了...

    gisapp:扩展QGIS Web客户端

    它将QGIS项目转换为带有查看,导航,搜索和打印数据的工具的Web和Mobile GIS应用程序。 最重要的是,还有一个PostgreSQL管理数据库,用于存储项目,层,用户以及一些不在QGIS项目中的特定设置。 数据库管理和项目...

    BAMS-JAVA快速开发框架 2.5

    BAMS 2.5 更新日志:2015-07-091、修复了日志无法输出的BUG,增加slf4j lo4j绑定,删除commons-logging,统一通过slf4j使用log4j2、更新 Excel的导入导出功能,使用SpingMVC代替Servlet接收请求,简化代码3、将Word...

Global site tag (gtag.js) - Google Analytics