用EXT写大型应用(Writing a Big Application in Ext)

简介: Tutorial:Writing a Big Application in Ext From Learn About the Ext JavaScript Library Jump to: navigation, search Summary: This tutorial helps you to write a big application in Ext.

Tutorial:Writing a Big Application in Ext

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: This tutorial helps you to write a big application in Ext.
Author: Jozef Sakalos
Published: April 12, 2008
Ext Version: 2.0+
Languages: en.pngEnglish

Contents

[hide]

Preface

I have decided to write this article for those users of Ext 2.x that have already grown up from children's diapers of 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 the number of solutions to it is equal to the number of people that tackle it. The way I am going to describe in the following text is not the only one, and I 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 maintainable --- 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 big application, right?

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

The answer to both above statements is Jein. When does the application become 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 a number of files or 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 DocumentRoot 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 when you, for example, download a new Ext version to a real directory, then you just 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, just change the link back.

  • css will hold all your stylesheets. If you have global stylesheets with company colors or fonts you can create the css directory as a link too.
  • ext link you your Ext JS Library tree as described above
  • img link to your images. It can contain an 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:

<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="./js/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.


js/application.js

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

// 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 the path to a 1x1 px 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, if any, into this file. If you need only a couple of rules it may seem as unnecessary to create a separate file for them, and it looks like a better idea to put them into <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 have to solve some rendering problems and you won't know where the styles are.

The wrong way

What does normally follow 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 couldn't we write it in index.html in the first place?

The right way: Break it apart

Even the most complex whole consists of smaller systems 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 these parts, components and relationships between them.

So, sit down, think it over, draw a sketch, make a list, whatever, but the result has to be that you know the components --at least the 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 extension 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:

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:

Application.PersonnelGrid = Ext.extend(Ext.grid.GridPanel, {
     border:false
    ,initComponent:function() {
        Ext.apply(this, {
             store:new Ext.data.Store({...})
            ,columns:[{...}, {...}]
            ,plugins:[...]
            ,viewConfig:{forceFit:true}
            ,tbar:[...]
            ,bbar:[...]
        });
 
        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 are we doing here? We're extending Ext.grid.GridPanel creating a new class (extension) Application.PersonnelGrid, and we are registering it as a 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:

var pg = new Application.PersonnelGrid();

or using its xtype (lazy instantiation):

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 the 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.js></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 into the ./js directory, include them in index.html and build our application from instances of them, as a 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 a 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?

In 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 the center region. We just cannot use it without the center, as then we have no component to create tabs in.

If we put it in the center, the result is the 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. An 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 file, and to minify it with some of the JavaScript minifying or compression tools. Also, you do not need the debug version of the Ext library for production systems.

On a production system we would include:

  • ext-all.js
  • app-all.js and
  • application.js

For additional information about minimizing your source and creating combined build files, we have another tutorial that covers this topic in depth.

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!

See also

相关文章
|
4天前
|
人工智能 自然语言处理 Shell
🦞 如何在 Moltbot 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
🦞 如何在 Moltbot 配置阿里云百炼 API
|
3天前
|
人工智能 JavaScript 应用服务中间件
零门槛部署本地AI助手:Windows系统Moltbot(Clawdbot)保姆级教程
Moltbot(原Clawdbot)是一款功能全面的智能体AI助手,不仅能通过聊天互动响应需求,还具备“动手”和“跑腿”能力——“手”可读写本地文件、执行代码、操控命令行,“脚”能联网搜索、访问网页并分析内容,“大脑”则可接入Qwen、OpenAI等云端API,或利用本地GPU运行模型。本教程专为Windows系统用户打造,从环境搭建到问题排查,详细拆解全流程,即使无技术基础也能顺利部署本地AI助理。
4425 7
|
9天前
|
人工智能 API 开发者
Claude Code 国内保姆级使用指南:实测 GLM-4.7 与 Claude Opus 4.5 全方案解
Claude Code是Anthropic推出的编程AI代理工具。2026年国内开发者可通过配置`ANTHROPIC_BASE_URL`实现本地化接入:①极速平替——用Qwen Code v0.5.0或GLM-4.7,毫秒响应,适合日常编码;②满血原版——经灵芽API中转调用Claude Opus 4.5,胜任复杂架构与深度推理。
|
3天前
|
人工智能 JavaScript API
零门槛部署本地 AI 助手:Clawdbot/Meltbot 部署深度保姆级教程
Clawdbot(Moltbot)是一款智能体AI助手,具备“手”(读写文件、执行代码)、“脚”(联网搜索、分析网页)和“脑”(接入Qwen/OpenAI等API或本地GPU模型)。本指南详解Windows下从Node.js环境搭建、一键安装到Token配置的全流程,助你快速部署本地AI助理。(239字)
2756 16
|
3天前
|
机器人 API 数据安全/隐私保护
只需3步,无影云电脑一键部署Moltbot(Clawdbot)
本指南详解Moltbot(Clawdbot)部署全流程:一、购买无影云电脑Moltbot专属套餐(含2000核时);二、下载客户端并配置百炼API Key、钉钉APP KEY及QQ通道;三、验证钉钉/群聊交互。支持多端,7×24运行可关闭休眠。
3132 4
|
3天前
|
人工智能 安全 Shell
在 Moltbot (Clawdbot) 里配置调用阿里云百炼 API 完整教程
Moltbot(原Clawdbot)是一款开源AI个人助手,支持通过自然语言控制设备、处理自动化任务,兼容Qwen、Claude、GPT等主流大语言模型。若需在Moltbot中调用阿里云百炼提供的模型能力(如通义千问3系列),需完成API配置、环境变量设置、配置文件编辑等步骤。本文将严格遵循原教程逻辑,用通俗易懂的语言拆解完整流程,涵盖前置条件、安装部署、API获取、配置验证等核心环节,确保不改变原意且无营销表述。
1816 3
|
4天前
|
存储 安全 数据库
使用 Docker 部署 Clawdbot(官方推荐方式)
Clawdbot 是一款开源、本地运行的个人AI助手,支持 WhatsApp、Telegram、Slack 等十余种通信渠道,兼容 macOS/iOS/Android,可渲染实时 Canvas 界面。本文提供基于 Docker Compose 的生产级部署指南,涵盖安全配置、持久化、备份、监控等关键运维实践(官方无预构建镜像,需源码本地构建)。
2178 7
|
12天前
|
JSON API 数据格式
OpenCode入门使用教程
本教程介绍如何通过安装OpenCode并配置Canopy Wave API来使用开源模型。首先全局安装OpenCode,然后设置API密钥并创建配置文件,最后在控制台中连接模型并开始交互。
5218 8
|
3天前
|
人工智能 应用服务中间件 API
刚刚,阿里云上线Clawdbot全套云服务!
阿里云上线Moltbot(原Clawdbot)全套云服务,支持轻量服务器/无影云电脑一键部署,可调用百炼平台百余款千问模型,打通iMessage与钉钉消息通道,打造开箱即用的AI智能体助手。
2347 18
刚刚,阿里云上线Clawdbot全套云服务!
|
3天前
|
人工智能 应用服务中间件 API
阿里云上线Clawdbot全套云服务,阿里云 Moltbot 全套云服务部署与使用指南
近期,阿里云正式上线 Moltbot(原名 Clawdbot)全套云服务,这套服务整合了 Agent 所需的算力、模型与消息应用能力,用户无需复杂配置,就能在轻量应用服务器或无影云电脑上快速启用 Moltbot,还能按需调用阿里云百炼平台的千问系列模型,同时支持 iMessage、钉钉等消息通道互动。相比传统本地部署方式,云服务方案不仅降低了硬件成本,还解决了网络依赖与多任务处理瓶颈,让普通用户也能轻松拥有专属 AI 助手。本文结合官方部署教程与全网实操经验,用通俗语言拆解从环境准备到功能使用的完整流程,同时说明核心组件的作用与注意事项,帮助用户顺利落地 Moltbot 云服务。
1761 0
阿里云上线Clawdbot全套云服务,阿里云 Moltbot 全套云服务部署与使用指南

热门文章

最新文章