ExtJs v4 – Ext.application , Ext.onReady

Confuse with the Ext.application(..) & Ext.onReady(..) ?? Read more …

Ext.application



What differences between Ext.application(..) & Ext.onReady(..)? Basically both syntax execute the code when the page is ready.

 
   Ext.application(…) take 1 parameter :

  •    config – Object

  Ext.application is a mvc approach to start up an application on a single page, it has the ability to ensure all the given dependencies   and configuration is loaded. Configuration refer to models: [''], controllers: [''] and etc javascript is required by the application.


Dig into source code

  1.  
  2. Ext.application=function(a){
  3.       Ext.require("Ext.app.Application");
  4.           Ext.onReady(function(){
  5.                Ext.create("Ext.app.Application",a)
  6.       })
  7. };
  8.  

From the source code and the explanation by sencha doc, Ext.onReady(..) performs add new listener to be executed when all required scripts are fully loaded.

 
   Ext.onReady(…) take 3 parameters :

  •    Function – a called back function
  •    Object – scope of execution for callback function
  •    Boolean – boolean value to represent wait for document ready




  Basically it doesn’t care the dependencies library as long as page is ready. Unless we provided Ext.required(..) before the     statement to ensure all the dependencies libary is loaded.


Example – Ext.onReady(..)

Ext.onReady(..) detect the page is loaded and DOM is ready for a UI. It has less memory consume and suit for simple UI display and has better performance.

  1.  
  2. Ext.onReady(function() {
  3.         var window = Ext.createWidget(‘window’, {
  4.                 width  : 500,
  5.                 height : 300,
  6.                 layout : {
  7.                         type : ‘border’,
  8.                         padding : 5
  9.                 },
  10.                 title  : ‘Hello dialog’,
  11.                 items  : [{
  12.                         title : ‘Nagivateion’,
  13.                         collapsible : true,
  14.                         region : ‘west’,
  15.                         width : 200,
  16.                         html  : ‘Hello’,
  17.                         split : true
  18.                 },{
  19.                         title  : ‘TabPanel’,
  20.                         region : ‘center’
  21.                 }]
  22.         });
  23.         window.show();
  24. });
  25.  


Analyze Ext.onReady(..) behaviour

1) Open the firefox and install the firebug browse to the http://localhost/example/
2) Click on the console from the firebug you will notices a lot of the message are show.
3) Observe the console that all the required library will loaded when it’s needed.




Analyze Ext.application(..) behaviour

  1.  
  2. Ext.application
  3. ({
  4.         name : ‘Example’,       
  5.         launch: function() {
  6.                 var window = Ext.createWidget(‘window’, {
  7.                 width  : 500,
  8.                 height : 300,
  9.                 layout : {
  10.                         type : ‘border’,
  11.                         padding : 5
  12.                 },
  13.                 title  : ‘Hello dialog’,
  14.                 items  : [{
  15.                         title : ‘Nagivateion’,
  16.                         collapsible : true,
  17.                         region : ‘west’,
  18.                         width : 200,
  19.                         html  : ‘Hello’,
  20.                         split : true
  21.                 },{
  22.                         title  : ‘TabPanel’,
  23.                         region : ‘center’
  24.                 }]
  25.         });
  26.         window.show();
  27.         }
  28. });

Replace the code and observe the browser console again. Only ‘Ext.layout.container.Border’ is loaded in dynamic.




If you want to ensure everything is loaded, you can add the Ext.required(..) before the name attribute, observe the console again, no library will dynamic loading in this time.

  1. name : ‘Example’,
  2. requires  : [‘Ext.layout.container.Border’],
  3. launch: function() {

Conclusion

Use Ext.application if you have a mvc application and display on the single page.

Use Ext.onReady(..) for performance and only few component display. Such as with any existing HTML page you had for simple UI interaction.

A typical example are show in below “Button click” on this page

  1.  
  2. Ext.get(‘ext’).on(‘click’, function(){
  3.         alert("You clicked the button");
  4. });
  5.  


  1.  
  2. Ext.onReady(function() {
  3.     Ext.get(‘window’).on(‘click’, function(){
  4.          var win = new Ext.Window({
  5.             width:400,
  6.             height:200,
  7.             title:"Ext Window",
  8.             html: "  ExtJs window "
  9.         });
  10.         win.show();
  11.     });
  12. });
  13.  

Download the example here

You can leave a response, or trackback from your own site.

Leave a Reply

Security Code: