ext5 - 异步加载要使用的自定义类

利用ext良好的扩展性,咱们很容易定义本身的组件。通常这样组件的代码都是一个单独的文件,在要用的时候就加载进来。长此以往,这样自定义的组件多了一样组件所对的文件也多了起来,这时若是咱们手动的去加载js文件,不只界面变得混乱,并且还会浪费一些带宽。下面的就是利用Ext.Loader.setConfig动态的加截这些文件javascript

 下面myWin.js是自定义的组件,组件名称(不包括组件的命名空间)要和文件名一致:java

Ext.define("customComponent.myWin",{
	extend:'Ext.window.Window',
     title: 'Hello',
     height: 200,
     width: 400,
     layout: 'fit',
     items: {  // Let's put an empty grid in just to illustrate fit layout
         xtype: 'grid',
         border: false,
         columns: [{header: 'World'}],                 // One header just for show. There's no data,
         store: Ext.create('Ext.data.ArrayStore', {}) // A dummy empty data store
     },
	constructor: function (config) {
         this.callParent(arguments); // calls Ext.panel.Panel's constructor
         
     }
})

利用Ext.Loader.setConfig,设置要加载的资源位置。:this

Ext.Loader.setConfig({
	enabled:true,
	paths:{
		customComponent:'custom/win'
	}
});

如上customComponent 犹如是一个命名空间,而其值就是命名空间所映射的资源路径,下面咱们就能够建立customComponent.myWin 窗口了:code

Ext.onReady(function(){
    var win = Ext.create("customComponent.myWin");
	win.show();

})

当ext建立customComponent.myWin时发现没有这个类,就会解析这个类其中customComponent会被认做命名空间,并到Ext.Loader.setConfig中去匹配,发现路径是custom/win。而myWin就会被认作是文件名,最后的资源路径是./custom/win/myWin.js。Ext会自动的加载这个资源,这样咱们就不用手动的去指定路径了。记住Ext只是在建立该类时才去加载的文件的。ip

注意:Ext.Loader.setConfig调用要在Ext.onRead前资源

相关文章
相关标签/搜索