PDA

View Full Version : How to load .js file



zzhouag
27 Oct 2012, 1:20 AM
I want to load .js file when I need to, not pre-load in html, and how to achieve that?

skirtle
27 Oct 2012, 4:16 PM
You can do it using require or syncRequire:

http://docs.sencha.com/ext-js/4-1/#!/api/Ext-method-require
http://docs.sencha.com/ext-js/4-1/#!/api/Ext-method-syncRequire

I can't think why you'd want to though, it could hurt performance quite badly.

zzhouag
27 Oct 2012, 7:45 PM
Thank you very much, because I want to try to load the multi language i18n js file and the language strings mapping js file when the component is created.

If I pre-load all these file I can achieve my goal, but I think each time there are some components may not be used, so is it better to load when needed than pre-load all the files?

The i18n file

String.prototype.i18n = function(){
var result = {};
var str = eval('string.'+lang);
if (this in str){
result = eval('str.'+ this);
}

...
return result;
};

And the string file:
var lang = "zh_CN";
var string = {};
string.zh_CN={
'About': '??',
...
}

They are in two separated js files, And I want to use them like:
{
xtype: 'linkbutton',
buttontext: 'About'.i18n(),
itemId: 'aboutClickLink',
cls: 'about'
}

But I use Ext.Loader.require it still says can't fine the i18n() functions. Thank you for your helps.

zzhouag
27 Oct 2012, 7:59 PM
Thank you, I put them in one file and did it use syncRequire