Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a third party library to ext js application lazily (that is only when it is required)

I want to add a third party js to a sencha application, lazily, only when rendering a new window, for example on clicking a button, a new window is popped up, and when this happens the d3.js library js is also loaded.

For loading a new file in Ext js itself, we can use requires key:

requires : ['MyApp.stores.UserStore']

as described in extjs documentation. Can we do something like this for third party libraries also?

P.S. - I am using Ext Js 4.1.0

like image 225
rubyprince Avatar asked Jan 14 '13 09:01

rubyprince


People also ask

What is third party library in JavaScript?

Third-party JavaScript applications are self-contained components, typically small scripts or widgets, that add functionality to websites. As the name implies, they're offered by independent organizations, with code and asset files served from a remote web address.

What is requires ExtJS?

You can look at requires as a way to tell ExtJS: "When you construct an object of this class, please make sure to dynamically load the required scripts first".

Is ExtJS single page application?

Ext JS by Sencha is becoming the ExtJS is a rich, compelling and intuitive open source framework best suited for building single page applications. It offers very rich set of UI components and thus giving a tough competition to other JavaScript frameworks like AngularJS or ReactJS.

How does JavaScript ext work?

It has the path defined for all the other files used in that application such as store, view, model, require, mixins. View. js − It contains the interface part of the application, which shows up to the user. Ext JS uses various UI rich views, which can be extended and customized here according to the requirement.


1 Answers

Update

You should be able to load nearly anything with Ext.Loader.loadScript(options) The method also provides a callback which is called when the loading is done.


Basically you can load anything as long as it is a single ExtJS class per file (I must admit that I never tried anything else!). You can register additional paths using

Ext.Loader.setPath('EL','your/path/name'); // not done in the example below!

and require something at runtime is quite easy as long as the loader is configured for that. Following is a example which loads the Ext.ux.statusbar.StatusBar at runtime from another domain and a complete new path. Here's the JSFiddle - just click the button and the class get loaded and after that applied with addDocked

Ext.Loader.setPath(  'Ext', 'http://docs.sencha.com/ext-js/4-1/extjs-build/examples');
Ext.create('Ext.Panel', {
    width: 200,
    height: 200,
    renderTo: Ext.getBody(),
    tbar: {
      xtype: 'statusbar',
      statusAlign: 'right',
        items: [
          {
            xtype: 'button', 
            text: 'show window', 
            id: 'ani-target', 
            handler: function(btn) { 
              if (btn.up('panel').down('window').isVisible()) {
                btn.up('panel').down('window').hide();
                btn.setText('maximize');
              }else {
                btn.up('panel').down('window').show();
                btn.setText('minimize');
                Ext.define('Ext.ux.custom.StatusBar',{
                  extend: 'Ext.ux.statusbar.StatusBar',
                  alias: 'widget.cstatus',
                  requires: ['Ext.ux.statusbar.StatusBar'],
                  text: 'Ready',
                  initComponent: function() {
                    this.callParent(arguments);
                  }
                });
                btn.up('panel').down('window').addDocked({ xtype: 'cstatus'});
              } 
            }
          }
        ],
    },
  items: [{
    xtype: 'window',
    closable: false,
    width: 100,
    height: 100,
    id: 'demo-win',
    animateTarget: 'ani-target'
  }]
})
like image 200
sra Avatar answered Oct 21 '22 15:10

sra