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

如何使用ExtJS构建应用系统

阅读更多
原文地址:http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/
Writing a Big Application in Ext (Part 1)
12. April 2008 – 23:20

Preface
I have decided to write this article for those users of Ext 2.x that have already grown up from having one HTML page with embedded script that creates one simple window or form, for those who are already decided that Ext is the way and for those who are fighting with too long files hardly to search in and feeling that their applications need a structure.

The number of approaches to a problem and number of solutions of it is equal to number of people that tackle it. The way I am going to describe in the following text in not the only one and do not want to say that either an application is going to be written my way or it is not good. Nothing like that.

What I do want to say is that this approach is workable, neatly structured, easily maintable, simply stated: It works!



What is “A Big Application”?
If you have a Viewport with BorderLayout, a grid and a form all in one file it certainly is not the big application, right?. If you have tens of windows each with a grid, form or border layout in it in tens of files it certainly is the bit application, right?

(Germans have very nice word: Jein which is combination of Ja = Yes and Nein = No.)

The answer to both above statement is Jein. When the application becomes big, then? The answer is simple: It becomes big when you feel it is big. It is the point when you start to have troubles to orient yourself in number of files or in you have troubles to find a specific place in one file, when you cease to understand relations of components, etc. I am writing you here but imagine when a 2-3 grades less experienced programmer starts to have this feelings.

We can safely state that each application is big as also a small application deserves to be well written and it may likely become really big as we start to add new features, write new lines of code, new CSS rules, etc.

The best and the safest state of the mind at the start of a new application is: I’m starting the big application!



Files and Directories
These we need to organize first. There is always a ServerRoot directory configured in Apache or another HTTP server so all subdirectories I’ll describe later are relative to it.

Recommended directory structure:

./css (optionally link)
./ext (link)
./img (link)
./js
index.html


Link in the above structure means a soft link pointing to a real directory where files are stored. The advantage is that you, for example, download new Ext version to a real directory then you change the link above to point there and without changing a line in your application you can test if everything works also with this new version. If yes, keep it as it is, if no, you just change the link back.

css will hold all your stylesheets. If you have global stylesheets with company colors or fonts you can create css directory as link too.
ext link you your Ext JS Library tree as described above
img link to your images. It can contain icons subdirectory as well.
js will hold all javascript files the Application is composed of.
index.html HTML file that is an entry point of your application. You can name it as you want and you may need some more files for example for a login process. Anyway, there is one application entry point/file.
optionally you can create a directory or a link for your server side part of the application (I have ./classes). You can name it as you wish but consistently for all applications you write (./server, ./php are some good examples)


index.html
Minimal index.html file content is:

Code:
<html>   
<head>   
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   
  <link rel="stylesheet" type="text/css" href="./ext/resources/css/ext-all.css">   
  <link rel="stylesheet" type="text/css" href="./css/application.css">   
  <script type="text/javascript" src="./ext/adapter/ext/ext-base.js"></script>   
  <script type="text/javascript" src="./ext/ext-all-debug.js"></script>   
  <script type="text/javascript" src="./application.js"></script>   
  <title>A Big Application</title>   
</head>   

<body></body>  
</html>  



Although you can do with the above file I would recommend to add a descriptive header to not only this file but to all files you create. Also an “End of file” marker has its value. See File Patterns for example of such headers.



application.js
We need a file where the onReady function will be placed; let’s call it application.js. Minimum content is:

Code:
// vim: sw=4:ts=4:nu:nospell:fdc=4    
/**   
* An Application   
*   
* @author    Ing. Jozef Sakáloš   
* @copyright (c) 2008, by Ing. Jozef Sakáloš   
* @date      2. April 2008   
* @version   $Id$   
*   
* @license application.js is licensed under the terms of the Open Source   
* LGPL 3.0 license. Commercial use is permitted to the extent that the   
* code/component(s) do NOT become part of another Open Source or Commercially   
* licensed development library or toolkit without explicit permission.   
*   
* License details: http://www.gnu.org/licenses/lgpl.html   
*/   
     
/*global Ext, Application */   
     
Ext.BLANK_IMAGE_URL = './ext/resources/images/default/s.gif';    
Ext.ns('Application');    
     
// application main entry point    
Ext.onReady(function() {    
     
    Ext.QuickTips.init();    
     
    // code here    
     
}); // eo function onReady    
     
// eof   





Your header and footer may vary but sure you need to set Ext.BLANK_IMAGE_URL to point to your server. This is path to 1×1px transparent image file that is used by Ext as an image placeholder and if it points to an invalid location you can get various rendering problems such as missing combo trigger images, missing icons or similar.

You may also need to create a new global object variable for your application (here it is Application).

What you need for sure is Ext.onReady that is the main application entry point – the place where you write your application.



css/application.css
You will put your css stylesheet to this file, if any. If you need only a couple of rules it may seem as unnecessary to create a separate file for them and it looks like the better idea to put them to <style> tags in the page head.

The reverse is true, remember you’re writing a big application, so everything has to have its place. If you put styles in the page head sooner or later you will solve some rendering problems and you won’t know where the styles are.



The wrong way
What normally follows when we have all basics in as we have at this point? Let’s begin writing. So we sit down and we start to write:

var vp = new Ext.Viewport({
layout:'border'
    ,items:[
        new Ext.grid.GridPanel({
            store:new Ext.data.Store({
                proxy:new Ext.data.HttpProxy({ ...


Wait a minute. This way we will have 10,000 lines in our application.js very soon and that is what we want last. Obviously, some step is missing as if we’re going to create such a big file why we couldn’t write it in index.html in the first place?



The right way: Break it apart
Even the most complex whole consists of smaller system which consist of smaller parts which consist of some elements. Your to-be-written big application is not an exception. Now it is the time to identify this parts, components and relationships between them.

So, sit down, think it over, draw a sketch, make a list, whatever but result has to be that you know the components, at least most important ones, your application will consist of.



Pre-configured classes
Now, that you are done with application analysis and identifying its components you can start to write the first one. But how? The best approach is to write extension classes of Ext components that have all configuration options, otherwise passed as the configuration object, built-in. I call such extensions pre-configured classes as they rarely add much functionality to base Ext ones but the main purpose of having them is to have them configured. For example, to have a “Personnel” grid with personnel specific column model, store, sorting options, editors, etc.

If we had such, our configuration of a Window could look like:

Code:
var win = new Ext.Window({    
     title:'Personnel'   
    ,widht:600    
    ,height:400    
    ,items:{xtype:'personnelgrid'}    
});    
win.show();   


Writing a pre-configured class
Let’s take an example to discuss:

Code:
Application.PersonnelGrid = Ext.extend(Ext.grid.GridPanel, {    
     border:false   
    ,initComponent:function() {    
        var config = {    
           store:new Ext.data.Store({...})    
          ,columns:[{...}, {...}]    
          ,plugins:[...]    
          ,viewConfig:{forceFit:true}    
          ,tbar:[...]    
          ,bbar:[...]    
        }; // eo config object    
     
        // apply config    
        Ext.apply(this, Ext.apply(this.initialConfig, config));    
     
        Application.PersonnelGrid.superclass.initComponent.apply(this, arguments);    
    } // eo function initComponent    
     
    ,onRender:function() {    
        this.store.load();    
     
        Application.PersonnelGrid.superclass.onRender.apply(this, arguments);    
    } // eo function onRender    
});    
     
Ext.reg('personnelgrid', Application.PersonnelGrid);    

 

What we’re doing here? We’re extending Ext.grid.GridPanel creating the new class (extension) Application.PersonnelGrid and we are registering it as the new xtype with name personnelgrid.

We are giving the general grid panel all the configuration options needed to become the specific personnel grid. From this point on we have a new component, a building block for our application that we can use everywhere (window, border panel region, standalone) where the list of personnel is needed. We can create it either as:

Code:
var pg = new Application.PersonnelGrid();    
     
// or using it's xtype    
var win = new Ext.Window({    
     items:{xtype:'personnelgrid'}    
    ,....    
});   


Organizing pre-configured classes
The code above does not need to and should not run within the onReady function because it has nothing to do with DOM structure; it only creates a new javascript object. Therefore it can and it should be written in a separate file (js/Application.PersonnelGrid.js) and it can and must be included in the index.html header as:

<script type="text/javascript" src="./js/Application.PersonnelGrid"><script>So far so good, we have almost everything in place and (almost) all we need to do more is to continue writing our pre-configured classes, put them in ./js; directory, include them in index.html and build your application from instances of them as puzzle is assembled from pieces.

Looks good, yeah?

Anyway, there is a bit more to it.



Inter component communication
Imagine that we need a border layout with a link list in the west and tab panel in the center region. Clicking a link in the west would create a new tab in the center. Now, where should we put the logic of it, event handler and creation routine? In the west, or in the center?

Neither of them. Why? If we have a pre-configured class that creates and displays the west link list and we put the above logic in it it can no longer exist without center region. We just cannot use it without center as then we have no component to create tabs in.

If we put it in center, the result is same: center cannot exist without west.

The only component that should be aware of the existence of both west and center panels is their container with the border layout and this is the only right place where to put inter-component communication.

What should we do then? The container (with border layout) should listen to events fired by the west and it should create tabs in the center as responses to these clicks. The example of the component communication written this way can be found here: Saki’s Ext Examples



Production Systems
As we keep on writing our application we happen to have large number of included javascript files very soon (I have around 80 includes in one application at present and this number grows every day). This can degrade performance of a production system.

The best way to solve it is to concatenate all javascript files in the right order to create one big and to minify it with some of the javascript minfying or compression tools. Also, you do not need debug version of Ext Library for production systems.

We would include:

ext-all.js
app-all.js and
application.js
on a production system.



Conclusion
It’s almost all there is to it… There are specific techniques for specific Ext classes, there is a lot of another server and client side know-how but the above is the overall concept.

Happy coding!
分享到:
评论

相关推荐

    extjs做的一个桌面应用系统

    EXTJS是一种基于JavaScript的...综上所述,EXTJS和DWR结合使用,可以构建出高度交互、用户体验良好的桌面应用系统。开发者需要对这两项技术有深入理解,才能充分利用它们的优势,打造出功能齐全、性能卓越的Web应用。

    使用ExtJs构建树形菜单功能

    ### 使用ExtJs构建树形菜单功能详解 #### 一、引言 在现代Web应用程序开发中,树形菜单是一项常用且重要的功能。它能够帮助用户更直观地理解数据层次关系,尤其是在展示组织架构、文件系统或者任何有层级结构的...

    extjs仿桌面系统

    EXTJS是一种基于JavaScript的富客户端应用开发框架,由Sencha公司...通过熟练掌握EXTJS,开发者可以创建出具有高度交互性和用户友好性的Web应用程序,即使是在浏览器环境中,也能提供近似于桌面操作系统的使用感受。

    使用Extjs写的简单酒店管理系统

    【标题】:使用Extjs构建的简易酒店管理系统 在IT行业中,开发用户界面时经常会用到JavaScript库,其中ExtJS是一个强大的前端框架,用于构建功能丰富的、交互式的Web应用程序。本项目“使用Extjs写的简单酒店管理...

    ExtJS Web应用程序开发指南(第2版).pdf 高清下载

    3. **参考案例研究**:网上有许多使用ExtJS构建的真实案例,通过分析这些案例,可以学到很多实战经验和技巧。 4. **参与社区讨论**:加入ExtJS的社区论坛或社交媒体群组,与其他开发者交流心得,解决遇到的问题。 ...

    Extjs酒店管理系统

    在"Extjs酒店管理系统"中,这一技术被充分利用,构建出直观且易于操作的界面。通过Extjs,开发者能够轻松创建数据驱动的网格、表单、菜单、工具栏等,极大地提高了系统的交互性和用户体验。此外,Extjs 2.0支持AJAX...

    ExtJs教学管理系统

    "ExtJs教学管理系统"是一个基于ExtJs框架和.Net后端技术构建的应用程序,旨在提供一套教育机构或学校使用的教学管理解决方案。此系统可能包括学生管理、课程管理、成绩管理、教师管理等多个模块,以帮助教育工作者...

    ExtJS 物流管理系统源代码

    1. ExtJS:ExtJS是一个强大的JavaScript库,用于构建富互联网应用程序(RIA)。它提供了丰富的组件模型,包括表格、面板、表单、树等,可以创建复杂的用户界面。在物流管理系统中,ExtJS可能用于构建交互式的数据...

    ExtJs3 演示系统

    ExtJs3是一个强大的JavaScript框架,主要用于构建富客户端的Web应用程序。...通过分析和研究这个系统,开发者可以掌握如何使用ExtJs3构建复杂且功能丰富的前端应用,并为实际项目提供有价值的参考。

    ExtJS Web应用程序开发指南(第2版).zip

    ExtJS是一种基于JavaScript的开源富客户端框架,专用于构建交互式和桌面级的Web应用程序。在《ExtJS Web应用程序开发指南(第2版)》中,开发者可以深入了解如何利用这个强大的框架来创建功能丰富的Web应用。这本书...

    Extjs文件管理系统

    ExtJS 文件管理系统是一款基于 ExtJS 框架构建的高效、功能丰富的文件管理应用程序。它提供了用户友好的界面,使得用户能够轻松地进行文件的压缩、解压以及搜索操作。ExtJS 是 Sencha 公司推出的一个强大的 ...

    jsp extjs Efs管理系统

    **JSP与EXTJS在EFS管理系统的应用** 在IT领域,JSP(JavaServer Pages)和EXTJS是两种常用的技术,它们在构建Web应用程序时发挥着重要作用。本项目"jsp extjs Efs管理系统"结合了这两者的优点,提供了一个完整的、...

    extjs信息系统extjs信息系统

    EXTJS是一种基于JavaScript的开源富客户端框架,专用于构建企业级Web应用程序。它提供了一套完整的组件库,包括数据网格、表单、图表等,使得开发者可以构建出交互性强、用户体验良好的网页应用。EXTJS信息系统显然...

    EXTJS图书管理系统页面(JAVA)

    EXTJS是一种基于JavaScript的开源富客户端框架,专用于构建交互式、数据驱动的Web应用程序。在"EXTJS图书管理系统页面(JAVA)"这个项目中,我们主要关注的是EXTJS在图书管理系统中的应用,以及它与Java后端的交互。...

    合同管理系统 extjs开发的 让大家一起学习

    本系统采用EXTJS进行前端开发,EXTJS是一款强大的JavaScript组件库,以其丰富的UI组件和数据绑定机制,为开发高质量的Web应用提供了便利。 EXTJS的核心特性包括: 1. **组件化设计**:EXTJS以组件为中心,提供了...

    extjs做的图书管理系统

    本文将详细介绍一个使用ExtJS开发的图书管理系统,该系统同时结合了Spring和iBATIS作为其核心架构组件。 首先,ExtJS是一个流行的JavaScript库,用于构建桌面级的Web应用程序。它提供了丰富的UI组件,如表格、窗口...

    extjs构建资源文件与 官方Demo

    EXTJS 2.2.1的构建资源包括了所有必要的JavaScript库、CSS样式表、图像和其他支持文件,这些是构建EXTJS应用的基础。主要知识点如下: 1. **核心库**:EXTJS的核心库文件(如`ext-all.js`或`ext-base.js`)包含了...

    Extjs5.0从入门到实战开发信息管理系统

    ExtJS 5.0 是一个强大的JavaScript框架,用于构建企业级的富客户端应用程序。这个框架提供了丰富的组件库,包括网格、表单、菜单、工具栏等,使得开发者能够创建功能完善的Web应用,无需关注底层的HTML和CSS。本教程...

    基于ExtJS做的投票系统

    ExtJS是一个强大的JavaScript库,专为构建富客户端应用程序而设计。在投票系统中,它用于创建交互式和动态的用户界面: 1. **组件模型**:ExtJS提供了丰富的组件库,如表格、面板、窗口、按钮等,方便快速构建复杂...

    extjs4_任务调度管理系统

    EXTJS4是一款强大的JavaScript前端框架,它提供了丰富的UI组件和交互模式,被广泛应用于构建功能完善的Web应用程序,特别是对于企业级的管理系统的开发,EXTJS4更是表现出色。本文将深入探讨如何利用EXTJS4开发任务...

Global site tag (gtag.js) - Google Analytics