Ext.Loader is the heart of the new dynamic dependency loading capability in Ext JS 4+. It is most commonly used via the Ext.requireshorthand. Ext.Loader supports both asynchronous and synchronous loading approaches, and leverage their advantages for the best development flow. We'll discuss about the pros and cons of each approach:

Ext.Loader是Ext JS4动态依赖加载能力的核心。最常见的情况是通过Ext.require使用它。Ext.Loader同时支持同步和异步加载方式。这里,我们将讨论这两种加载方式的优缺点。

Asynchronous Loading  异步加载

  • Advantages:  优势

    • Cross-domain  跨域
    • No web server needed: you can run the application via the file system protocol (i.e: file://path/to/your/index .html)  不需要web服务器:你能通过文件系统协议运行程序。比如file://path/to/your/index.html
    • Best possible debugging experience: error messages come with the exact file name and line number  舒服的调试体验:错误信息将返回确切的文件名字和行数。
  • Disadvantages: 缺点

    • Dependencies need to be specified before-hand   依赖必须事先指定

Method 1: Explicitly include what you need:   方法一:明确包含你想要的

// Syntax
Ext.require({String/Array} expressions); // Example: Single alias
Ext.require('widget.window'); // Example: Single class name
Ext.require('Ext.window.Window'); // Example: Multiple aliases / class names mix
Ext.require(['widget.window', 'layout.border', 'Ext.data.Connection']); // Wildcards
Ext.require(['widget.*', 'layout.*', 'Ext.data.*']);

Method 2: Explicitly exclude what you don't need: 方法二,明确排除你不想要的

// Syntax: Note that it must be in this chaining format.
Ext.exclude({String/Array} expressions)
.require({String/Array} expressions); // Include everything except Ext.data.*
Ext.exclude('Ext.data.*').require('*'); // Include all widgets except widget.checkbox*,
// which will match widget.checkbox, widget.checkboxfield, widget.checkboxgroup, etc.
Ext.exclude('widget.checkbox*').require('widget.*');

Synchronous Loading on Demand   同步加载

  • Advantages: 优势

    • There's no need to specify dependencies before-hand, which is always the convenience of including ext-all.js before  它不需要事先指明依赖,事先包含ext-all.js是很方便的。
  • Disadvantages:缺点

    • Not as good debugging experience since file name won't be shown (except in Firebug at the moment) 调试体验不好,除非用Firebug调试,否则出错的文件的名字不会显示。
    • Must be from the same domain due to XHR restriction不能跨域请求,因为XHR的限制必须是相同的域名。
    • Need a web server, same reason as above   并且因为这个原因,必须有web服务。

There's one simple rule to follow: Instantiate everything with Ext.create instead of the new keyword

可以遵守一个简单的法则:用Ext.create代替new关键字来实例化对象。

Ext.create('widget.window', { ... }); // Instead of new Ext.window.Window({...});

Ext.create('Ext.window.Window', {}); // Same as above, using full class name instead of alias

Ext.widget('window', {}); // Same as above, all you need is the traditional `xtype`

Behind the scene, Ext.ClassManager will automatically check whether the given class name / alias has already existed on the page. If it's not, Ext.Loaderwill immediately switch itself to synchronous mode and automatic load the given class and all its dependencies.

在后台,Ext.ClassManager会自动检查给定的类名或别名是否在页面已经存在。如果没有,Ext.Loader将会立即把它调整为同步模式,自动加载给定的类和它所有的依赖。

Hybrid Loading - The Best of Both Worlds  混合加载

It has all the advantages combined from asynchronous and synchronous loading. The development flow is simple:

混合加载方式可以结合同步和异步加载的优势。开发流程非常简单:

Step 1: Start writing your application using synchronous approach.

第一步:用同步的方式写你的程序,

Ext.Loader will automatically fetch all dependencies on demand as they're needed during run-time. For example:

Ext.Loader将会自动按照需要获取所有的依赖,因为它们在运行时需要。例如

Ext.onReady(function(){
var window = Ext.widget('window', {
width: 500,
height: 300,
layout: {
type: 'border',
padding: 5
},
title: 'Hello Dialog',
items: [{
title: 'Navigation',
collapsible: true,
region: 'west',
width: 200,
html: 'Hello',
split: true
}, {
title: 'TabPanel',
region: 'center'
}]
}); window.show();
})

Step 2: Along the way, when you need better debugging ability, watch the console for warnings like these:

第二步:观看控制台的中如下的警告:

[Ext.Loader] Synchronously loading 'Ext.window.Window'; consider adding Ext.require('Ext.window.Window') before your application's code
ClassManager.js:432
[Ext.Loader] Synchronously loading 'Ext.layout.container.Border'; consider adding Ext.require('Ext.layout.container.Border') before your application's code

Simply copy and paste the suggested code above Ext.onReady, i.e:

在Ext.onReady上面添加加载依赖的代码:

Ext.require('Ext.window.Window');
Ext.require('Ext.layout.container.Border'); Ext.onReady(...);

Everything should now load via asynchronous mode.

这样,所有的东西都将通过异步的模式加载

Deployment   部署

It's important to note that dynamic loading should only be used during development on your local machines. During production, all dependencies should be combined into one single JavaScript file. Ext.Loader makes the whole process of transitioning from / to between development / maintenance and production as easy as possible. Internally Ext.Loader.history maintains the list of all dependencies your application needs in the exact loading sequence. It's as simple as concatenating all files in this array into one, then include it on top of your application.

一点很重要,动态加载只能在开发的时候在本机上使用。产品发布的时候,所有的依赖最好是组合成一个独一的JavaScript文件。Ext.Loader使项目从开发维护发布之间转换变得很容易。在内部,Ext.Loader.history控制了你的项目所有依赖的加载顺序的列表。把这个列表中的所有依赖压缩成一个,然后把它包含在你项目的最顶部。

This process will be automated with Sencha Command, to be released and documented towards Ext JS 4 Final.

这个处理过程将会使用SenchCommand自动完成。

ExtJS笔记 Ext.Loader的更多相关文章

  1. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  2. ExtJS笔记 Ext.data.Model

    A Model represents some object that your application manages. For example, one might define a Model ...

  3. extjs笔记

      1.    ExtJs 结构树.. 2 2.    对ExtJs的态度.. 3 3.    Ext.form概述.. 4 4.    Ext.TabPanel篇.. 5 5.    Functio ...

  4. Extjs4 -- Ext.loader命名空间的配置

    初次使用extjs4的版本,在配置学习Ext.Loader()进行js文件的动态加载机制,由于各种原因导致多次失败,纠结2天,现将解决时出现的问题及需要注意事项进行记录 开发环境myeclipse8. ...

  5. ExtJS笔记3 MVC Architecture

    MVC Architecture   MVC架构 Contents File Structure Creating the application in app.js Defining a Contr ...

  6. Ext.Loader

    Ext.Loader是Ext JS4动态加载的核心,等价于Ext.require简写. Ext.Loader支持异步和同步加载的方法. 异步 优点: 1.跨域 2.不需要web服务器 3.调试方便(可 ...

  7. Ext4报错Uncaught Ext.Loader is not enabled

    提示: Uncaught Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing requ ...

  8. sencha警告:[WARN][Anonymous] [Ext.Loader] Synchronously loading 'Ext.field.Text'

    chrome开发者工具下提示: [WARN][Anonymous] [Ext.Loader] Synchronously loading 'Ext.field.Text'; consider addi ...

  9. ExtJS学习-----------Ext.Object,ExtJS对javascript中的Object的扩展

    关于ExtJS对javascript中的Object的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

随机推荐

  1. Azkaban 2.5.0 搭建

    一.前言 最近试着参照官方文档搭建 Azkaban,发现文档很多地方有坑,所以在此记录一下. 二.环境及软件 安装环境: 系统环境: ubuntu-12.04.2-server-amd64 安装目录: ...

  2. uva-1339Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  3. 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法

    众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...

  4. Long-Short Memory Network(LSTM长短期记忆网络)

    自剪枝神经网络 Simple RNN从理论上来看,具有全局记忆能力,因为T时刻,递归隐层一定记录着时序为1的状态 但由于Gradient Vanish问题,T时刻向前反向传播的Gradient在T-1 ...

  5. RequireJS 循环依赖报 模块undefined 处理方案

    RequireJS 循环依赖 开始学习使用RequireJS之后做了几个小例子,之后想着把手头的项目也用RequireJS写一遍试试.感觉胜利就在前方了,忽然发现始终卡在一个问题上: 很常见的一个问题 ...

  6. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  7. 20161003 NOIP 模拟赛 T2 解题报告

    Weed duyege的电脑上面已经长草了,经过辨认上面有金坷垃的痕迹. 为了查出真相,duyege 准备修好电脑之后再进行一次金坷垃的模拟实验. 电脑上面有若干层金坷垃,每次只能在上面撒上一层高度为 ...

  8. 【bzoj1455】罗马游戏 可并堆

    2016-05-31  10:04:41 可并堆的裸题. 左偏树(小根堆为例 性质 1.满足堆的性质,每个节点权值小于左右儿子权值 2.每个节点有dis值,表示子树最浅的叶子深度加1 3.左子树dis ...

  9. Ninject使用demo

    public class HomeController : Controller { public ActionResult Index() { //核心对象 IKernel ninjectKerne ...

  10. BZOJ4519: [Cqoi2016]不同的最小割

    Description 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成 两个部分,如果结点s,t不在同一个部分中,则称这个划分是关于s,t的割.对于带权图来说,将 ...