View Full Version : How to Load Dinamically (On Demand) external javascript with ExtJS 4???

29 Oct 2011, 5:20 AM
Hi Everyone,

I want to load external javascript dynamically . I have tried with normal javascript

function loadjsfile(filename)
{ if (filetype=="js")
{ var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename) } if (typeof fileref!="undefined")
{ document.getElementsByTagName("head")[0].appendChild(fileref)
} } loadjscssfile("myscript.js")

But this code is not working properly ...

Any suggestions for this problem?

29 Oct 2011, 9:39 PM
There are two ways of doing this using Ext 4.

Using Ext's MVC architecture you can define the scripts you want to load within the Ext.Loader config. Say I want to load a custom store class. All of my files are located within the 'src' directory, and their folder structure matches the classname.

#File /src/data/Store.js
Ext.define('MyApp.data.Store', {});

To load that file within my app, you just have to set the loader 'paths' configuration property and then require the file.

enabled: true,
paths: {
MyApp : 'src'
Ext.require('MyApp.data.Store', function () {
// I can create the custom store object during this callback

Additionally, you should be able to just load arbitrary javascript files using the Ext.Loader class.

var url = "myfile.js";
var onload = function () {
// do something onload
var onerror = function () {
// do something onerror
var scope = this;
Ext.Loader.injectScriptElement(url, onload, onerror, scope);