dimitrij.zub
9 Aug 2008, 3:45 AM
Hi all.
I have developed this small piece of code to reduce the time my application needs to load at starttime and dynamically load modules if required.
So firt the code:
Ext.namespace('Ext.rt');
Ext.rt.LoadDialog = Ext.extend(Ext.Window, {
modal: true,
closable: false,
resizable: false,
draggable: false,
layout: 'fit',
hideMode: 'display',
progressBar: null,
steps: null,
status: null,
callback: null,
component: null,
dependency: null,
width: 300,
height: 35,
dependencies: null,
initComponent: function() {
this.dependencies = new Array();
this.progressBar = new Ext.ProgressBar({
id: Math.random()
});
Ext.apply(this, {
items : [this.progressBar]
});
this.on({
beforeshow : function(comp) {
comp.center();
}
});
Ext.rt.LoadDialog.superclass.initComponent.apply(this, arguments);
},
load: function(component, url, callback, steps) {
if (component)
this.dependencies.push([component, url]);
if (steps) {
this.steps = steps;
this.status = 0;
}
if (callback)
this.callback = callback;
var scripts = document.getElementsByTagName("script");
for (i=0;i<scripts.length;i++)
if (scripts[i].getAttribute('src')== url) {
if (component!=null)
this.onLoadComplete(component);
return;
}
this.show();
this.progressBar.updateProgress(this.status/this.steps, '#{global.loading_component}');
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
},
depends: function(component, url) {
var scripts = document.getElementsByTagName("script");
for (i=0;i<scripts.length;i++)
if (scripts[i].getAttribute('src')== url)
return;
this.dependencies.push([component, url]);
},
next: function() {
this.status++;
this.progressBar.updateProgress(this.status/this.steps);
},
done: function() {
this.progressBar.updateProgress(1.0);
this.hide();
},
onLoadComplete: function(component) {
this.next();
if (component) {
var slice = this.getIndex(component);
this.dependencies.splice(slice, 1);
}
if (this.dependencies.length == 0)
eval(this.callback+"()");
else {
this.load(null,this.dependencies[0][1]);
}
},
getIndex: function(component) {
for (var index = 0; index < this.dependencies.length; index++) {
if (this.dependencies[index][0] == component)
return index;
}
},
onFormActionStart: function(action) {
Ext.rt.Application.Loader.show();
Ext.rt.Application.Loader.status = Ext.rt.Application.Loader.steps-1;
if (action && action.options)
Ext.rt.Application.Loader.progressBar.updateProgress(Ext.rt.Application.Loader.status/Ext.rt.Application.Loader.steps, action.options.waitMsg);
else
Ext.rt.Application.Loader.progressBar.updateProgress(Ext.rt.Application.Loader.status/Ext.rt.Application.Loader.steps, '#{global.status_loading_data}');
},
onFormActionEnd: function(action, success) {
Ext.rt.Application.Loader.progressBar.updateProgress(1.0);
Ext.rt.Application.Loader.hide();
var options = null;
if (action)
options = action.options;
if(success){
if (options) {
if(options.reset)
this.reset();
Ext.callback(options.success, options.scope, [this, action]);
}
this.fireEvent('actioncomplete', this, action);
} else {
if (options)
Ext.callback(options.failure, options.scope, [this, action]);
this.fireEvent('actionfailed', this, action);
}
}
});
now how to use it:
First create a global loader in your applications onReady function:
Ext.rt.Application.Loader = new Ext.rt.LoadDialog();
Lets say you want to load the a module and once its loaded you want to run an action.
Ext.rt.Application.Loader.load(
"Ext.rt.Module", //the name of the module
"/Ext.rt.Module.js", //the location of the file
"Ext.rt.Module.onPlug", //the function to call once the module is completely loaded
5); //estimated steps of loading
will load the module. Now the module might have dependencies and how to know if the module is loaded at all and it should be working in all browsers!
you will need to extend your modules by those lines:
Ext.rt.Application.Loader.depends(
'Ext.rt.AnotherModule', //the module we depend on
"/Ext.rt.AnotherModule.js"); //the location of the dependency
Ext.rt.Application.Loader.onLoadComplete('Ext.rt.Module'); //inform the loader that this module is loaded.
You can go ahead and add more dependencies in any script you are depending on. Just trigger the dependencies before you inform about a sucessfull load of the module.
The loader will add those to a list of dependencies and add the script tags to your dom. Each time a module informs about its load to be complete, the dependency will be removed. If no more dependencies are to be loaded the callback will be run.
You are welcome to modify the code and post improvements.
Regards :)
I have developed this small piece of code to reduce the time my application needs to load at starttime and dynamically load modules if required.
So firt the code:
Ext.namespace('Ext.rt');
Ext.rt.LoadDialog = Ext.extend(Ext.Window, {
modal: true,
closable: false,
resizable: false,
draggable: false,
layout: 'fit',
hideMode: 'display',
progressBar: null,
steps: null,
status: null,
callback: null,
component: null,
dependency: null,
width: 300,
height: 35,
dependencies: null,
initComponent: function() {
this.dependencies = new Array();
this.progressBar = new Ext.ProgressBar({
id: Math.random()
});
Ext.apply(this, {
items : [this.progressBar]
});
this.on({
beforeshow : function(comp) {
comp.center();
}
});
Ext.rt.LoadDialog.superclass.initComponent.apply(this, arguments);
},
load: function(component, url, callback, steps) {
if (component)
this.dependencies.push([component, url]);
if (steps) {
this.steps = steps;
this.status = 0;
}
if (callback)
this.callback = callback;
var scripts = document.getElementsByTagName("script");
for (i=0;i<scripts.length;i++)
if (scripts[i].getAttribute('src')== url) {
if (component!=null)
this.onLoadComplete(component);
return;
}
this.show();
this.progressBar.updateProgress(this.status/this.steps, '#{global.loading_component}');
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
},
depends: function(component, url) {
var scripts = document.getElementsByTagName("script");
for (i=0;i<scripts.length;i++)
if (scripts[i].getAttribute('src')== url)
return;
this.dependencies.push([component, url]);
},
next: function() {
this.status++;
this.progressBar.updateProgress(this.status/this.steps);
},
done: function() {
this.progressBar.updateProgress(1.0);
this.hide();
},
onLoadComplete: function(component) {
this.next();
if (component) {
var slice = this.getIndex(component);
this.dependencies.splice(slice, 1);
}
if (this.dependencies.length == 0)
eval(this.callback+"()");
else {
this.load(null,this.dependencies[0][1]);
}
},
getIndex: function(component) {
for (var index = 0; index < this.dependencies.length; index++) {
if (this.dependencies[index][0] == component)
return index;
}
},
onFormActionStart: function(action) {
Ext.rt.Application.Loader.show();
Ext.rt.Application.Loader.status = Ext.rt.Application.Loader.steps-1;
if (action && action.options)
Ext.rt.Application.Loader.progressBar.updateProgress(Ext.rt.Application.Loader.status/Ext.rt.Application.Loader.steps, action.options.waitMsg);
else
Ext.rt.Application.Loader.progressBar.updateProgress(Ext.rt.Application.Loader.status/Ext.rt.Application.Loader.steps, '#{global.status_loading_data}');
},
onFormActionEnd: function(action, success) {
Ext.rt.Application.Loader.progressBar.updateProgress(1.0);
Ext.rt.Application.Loader.hide();
var options = null;
if (action)
options = action.options;
if(success){
if (options) {
if(options.reset)
this.reset();
Ext.callback(options.success, options.scope, [this, action]);
}
this.fireEvent('actioncomplete', this, action);
} else {
if (options)
Ext.callback(options.failure, options.scope, [this, action]);
this.fireEvent('actionfailed', this, action);
}
}
});
now how to use it:
First create a global loader in your applications onReady function:
Ext.rt.Application.Loader = new Ext.rt.LoadDialog();
Lets say you want to load the a module and once its loaded you want to run an action.
Ext.rt.Application.Loader.load(
"Ext.rt.Module", //the name of the module
"/Ext.rt.Module.js", //the location of the file
"Ext.rt.Module.onPlug", //the function to call once the module is completely loaded
5); //estimated steps of loading
will load the module. Now the module might have dependencies and how to know if the module is loaded at all and it should be working in all browsers!
you will need to extend your modules by those lines:
Ext.rt.Application.Loader.depends(
'Ext.rt.AnotherModule', //the module we depend on
"/Ext.rt.AnotherModule.js"); //the location of the dependency
Ext.rt.Application.Loader.onLoadComplete('Ext.rt.Module'); //inform the loader that this module is loaded.
You can go ahead and add more dependencies in any script you are depending on. Just trigger the dependencies before you inform about a sucessfull load of the module.
The loader will add those to a list of dependencies and add the script tags to your dom. Each time a module informs about its load to be complete, the dependency will be removed. If no more dependencies are to be loaded the callback will be run.
You are welcome to modify the code and post improvements.
Regards :)