Extjs4之前的版本沒有動態加載類的方式,這樣開發程序的時候加載不少的js會致使加載變慢,因爲本人一直使用extjs3的版本進行開發,因而簡單實現了一個動態加載類的管理器,使用方式與extjs4的方式有些相似,在每個類中須要使用其餘類的時候像java同樣先improt再使用。先看使用方式:java
- Ext.setPath("ThinkOA", "app");
- Ext.setPath("Ext.ux", "resources/ux");
- Ext.loadJs("ThinkOA/ns.js");
- Ext.loadJs("ThinkOA/constant.js");
-
- Ext.require("ThinkOA.Viewport");
- Ext.require("ThinkOA.LoginWindow");
- new ThinkOA.LoginWindow({
- isDebug: false,
- loginUrl: "login.do",
-
- listeners: {
- scope: this,
- submit: function(win, jsonObject) {
- if (jsonObject.success) {
- new ThinkOA.Viewport({
- });
- win.close();
- }else {
- Ext.MessageBox.alert("提示",jsonObject.message);
- }
- }
- }
-
- }).show();
![](http://static.javashuo.com/static/loading.gif)
js文件目錄結構以下:
web
按照這樣的方式,當須要引入一個類的時候直接調用Ext.require()方法便可,這樣在使用的時候就會動態去加載這個類,固然也能夠動態的去引入一個不是類的js文件,調用Ext.loadJs()便可實現動態加載,如今下面給出動態加載js實現的代碼,在這裏採用ajax去請求js文件,然而ajax的方式都是異步的,這樣就不能保證須要使用的類還沒加載完就執行後面的代碼,因而先實現一個同步的ajax方法,代碼以下:ajax
-
-
-
- Ext.apply(Ext.Ajax, {
-
-
-
- syncRequest : function(cfg) {
- cfg = Ext.apply({
- success : Ext.emptyFn,
- failure : Ext.emptyFn
- }, cfg)
- var obj;
- if (window.ActiveXObject) {
- obj = new ActiveXObject('Microsoft.XMLHTTP');
- } else if (window.XMLHttpRequest) {
- obj = new XMLHttpRequest();
- }
- obj.open('POST', cfg.url, false);
- obj.setRequestHeader('Content-Type',
- 'application/x-www-form-urlencoded');
- obj.send(cfg.params);
- var argument = cfg.argument || {};
- if (obj.status == 200) {
- cfg.success.call(cfg.scope || this, obj,argument);
- } else if(obj.status == 404){
-
- cfg.failure.call(cfg.scope || this, obj,argument);
- }else {
- cfg.failure.call(cfg.scope || this, obj,argument);
- }
-
- }
- });
有了此同步請求方法後,下面的js動態加載管理器就很容易實現,當加載一次的js文件就不會再加載,代碼:json
-
-
-
- Ext.JsMgr = Ext.extend(Object, {
- timeout : 30,
- scripts : {},
- disableCaching : true,
- paths : {},
- setPath : function(dest, target) {
- this.paths[dest] = target;
- },
- loadClass : function(className) {
- for (var p in this.paths) {
- className = className.replace(p, this.paths[p])
- }
- var jsUrl = className.split(".").join("/") + ".js";
- if (!this.scripts[className]) {
-
- Ext.Ajax.syncRequest({
- url : jsUrl,
- success : this.processSuccess,
- failure : this.processFailure,
- scope : this,
- timeout : (this.timeout * 1000),
- disableCaching : this.disableCaching,
- argument : {
- 'url' : className
- }
- });
- }
- },
- loadJavaScript : function(filepath) {
- var className = filepath.replace(".js","").split("/").join(".");
- this.loadClass(className);
- },
- processSuccess : function(response,argument) {
- this.scripts[argument.url] = true;
- window.execScript ? window
- .execScript(response.responseText) : window
- .eval(response.responseText);
- },
- processFailure : function() {
- }
- })
- Ext.JsMgr = new Ext.JsMgr();
-
- Ext.setPath = function(ns,path) {
- Ext.JsMgr.setPath(ns,path) ;
- }
- Ext.require = function() {
- Ext.JsMgr.loadClass(arguments[0]);
- };
- Ext.loadJs = function() {
- Ext.JsMgr.loadJavaScript(arguments[0])
- }
到此js加載管理器實現完成,目前只是先隨便寫了個哉項目中使用了下,還比較方便,再也不須要大量的引入js,這樣就能夠實現按需加載,只是目前採用同步加載方式效率不會過高,之後考慮改爲異步加載,提升加載速度。最後給出完整代碼:app
- (function() {
-
-
-
- Ext.apply(Ext.Ajax, {
-
-
-
- syncRequest : function(cfg) {
- cfg = Ext.apply({
- success : Ext.emptyFn,
- failure : Ext.emptyFn
- }, cfg)
- var obj;
- if (window.ActiveXObject) {
- obj = new ActiveXObject('Microsoft.XMLHTTP');
- } else if (window.XMLHttpRequest) {
- obj = new XMLHttpRequest();
- }
- obj.open('POST', cfg.url, false);
- obj.setRequestHeader('Content-Type',
- 'application/x-www-form-urlencoded');
- obj.send(cfg.params);
- var argument = cfg.argument || {};
- if (obj.status == 200) {
- cfg.success.call(cfg.scope || this, obj,argument);
- } else if(obj.status == 404){
-
- cfg.failure.call(cfg.scope || this, obj,argument);
- }else {
- cfg.failure.call(cfg.scope || this, obj,argument);
- }
-
- }
- });
- Ext.override(Ext.mgr.CompMgr,{
- getInstance : function(id, override){
- var instance, comp = this.get(id);
- if(comp){
- Ext.require(comp.className);
- instance = new (comp.getClass())(Ext.apply(comp.getConfig(), override));
- }
- return instance;
- }
- })
-
-
-
- Ext.JsMgr = Ext.extend(Object, {
- timeout : 30,
- scripts : {},
- disableCaching : true,
- paths : {},
- setPath : function(dest, target) {
- this.paths[dest] = target;
- },
- loadClass : function(className) {
- for (var p in this.paths) {
- className = className.replace(p, this.paths[p])
- }
- var jsUrl = className.split(".").join("/") + ".js";
- if (!this.scripts[className]) {
-
- Ext.Ajax.syncRequest({
- url : jsUrl,
- success : this.processSuccess,
- failure : this.processFailure,
- scope : this,
- timeout : (this.timeout * 1000),
- disableCaching : this.disableCaching,
- argument : {
- 'url' : className
- }
- });
- }
- },
- loadJavaScript : function(filepath) {
- var className = filepath.replace(".js","").split("/").join(".");
- this.loadClass(className);
- },
- processSuccess : function(response,argument) {
- this.scripts[argument.url] = true;
- window.execScript ? window
- .execScript(response.responseText) : window
- .eval(response.responseText);
- },
- processFailure : function() {
- }
- })
- Ext.JsMgr = new Ext.JsMgr();
-
- Ext.setPath = function(ns,path) {
- Ext.JsMgr.setPath(ns,path) ;
- }
- Ext.require = function() {
- Ext.JsMgr.loadClass(arguments[0]);
- };
- Ext.loadJs = function() {
- Ext.JsMgr.loadJavaScript(arguments[0])
- }
- })();
源碼下載地址:http://download.csdn.net/detail/ybhanxiao/7804811異步