Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Ext's loader to load non-ext scripts/object dynamically?

Tags:

extjs4

In my ExtJS 4.0.7 app I have some 3rd party javascripts that I need to dynamically load to render certain panel contents (some fancy charting/visualization widgets).

I run in to the age-old problem that the script doesn't finish loading before I try to use it. I thought ExtJS might have an elegant solution for this (much like the class loader: Ext.Loader).

I've looked at both Ext.Loader and Ext.ComponentLoader, but neither seem to provide what I'm looking for. Do I have to just "roll my own" and setup a timer to wait for a marker variable to exist?

like image 283
Brian Avatar asked Feb 24 '12 02:02

Brian


People also ask

What is Ext loader?

Files. Loader.js. Ext.Loader is the heart of the new dynamic dependency loading capability in Ext JS 4+. It is most commonly used via the Ext.require shorthand. Ext.Loader supports both asynchronous and synchronous loading approaches, and leverage their advantages for the best development flow.

What is the use of Ext?

Ext JS is a popular JavaScript framework which provides rich UI for building web applications with cross-browser functionality. Ext JS is basically used for creating desktop applications. It supports all the modern browsers such as IE6+, FF, Chrome, Safari 6+, Opera 12+, etc.


3 Answers

Here's an example of how it's done in ExtJS 4.1.x:

Ext.Loader.loadScript({
    url: '...',                    // URL of script
    scope: this,                   // scope of callbacks
    onLoad: function() {           // callback fn when script is loaded
        // ...
    },
    onError: function() {          // callback fn if load fails 
        // ...
    } 
});
like image 187
Joe Holloway Avatar answered Jan 17 '23 20:01

Joe Holloway


I've looked at both Ext.Loader and Ext.ComponentLoader, but neither seem to provide what I'm looking for

Really looks like it's true. The only thing that can help you here, I think, is Loader's injectScriptElement method (which, however, is private):

var onError = function() {
  // run this code on error
};
var onLoad = function() {
  // run this code when script is loaded
};
Ext.Loader.injectScriptElement('/path/to/file.js', onLoad, onError);

Seems like this method would do what you want (here is example). But the only problem is that , ... you know, the method is marked as private.

like image 42
Molecular Man Avatar answered Jan 17 '23 20:01

Molecular Man


This is exactly what newest Ext.Loader.loadScript from Ext.4-1 can be used for.

See http://docs.sencha.com/ext-js/4-1/#!/api/Ext.Loader-method-loadScript

like image 25
Krzysztof FF Avatar answered Jan 17 '23 20:01

Krzysztof FF