标签:
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同时支持同步和异步加载方式。这里,我们将讨论这两种加载方式的优缺点。
Advantages: 优势
file://path/to/your/index .html
) 不需要web服务器:你能通过文件系统协议运行程序。比如file://path/to/your/index.htmlDisadvantages: 缺点
// 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.*‘]);
// 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.*‘);
Advantages: 优势
Disadvantages:缺点
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将会立即把它调整为同步模式,自动加载给定的类和它所有的依赖。
It has all the advantages combined from asynchronous and synchronous loading. The development flow is simple:
混合加载方式可以结合同步和异步加载的优势。开发流程非常简单:
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(); })
第二步:观看控制台的中如下的警告:
[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.
这样,所有的东西都将通过异步的模式加载
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自动完成。
标签:
原文地址:http://www.cnblogs.com/jimcheng/p/4283029.html