Skip to main content

EXTJS4 mixins, dynamic loading Examples

List of classes to mix into other classes.

For example:

// app.js file 
// if Musician don't have a sing method?
Example1:
Ext.define('Musician', {
     mixins: ['CanSing']
});


Ext.define('CanSing', {
          sing: function() {
              console.log("sing method called in CanSing class...");
          }
     });
     

//  TODO - to mix in two classes, both of which define sing?
     Ext.define('Musician', {
      constructor:function(){
       console.log('Musician constructor');
      },
      initComponent:function(){
       console.log('Musician initCom');
      },
      mixins: {
             canSing: 'CanSing'
         },
         sing: function() {
          console.log("sing method called in Musician class..");
             // delegate singing operation to mixin
             this.mixins.canSing.sing.call(this);
         }
     });



// Example2:
Ext.define('CanPlayGuitar', {
     playGuitar: function() {
      console.log("playGuitar method called in CanPlayGuitar class...");
         }
});
     
Ext.define('CoolPerson', {
 constructor:function(){
  console.log('CoolPerson constructor');
 },
 initComponent:function(){
  console.log('CoolPerson initCom');
 },
 mixins: {
  canPlayGuitar:'CanPlayGuitar',
  canSing:'CanSing'
 },
 sing: function(){
  console.log("sing from coolperson...");
  this.mixins.canSing.sing.call(this);
  // this.sing();  // got to infinte loop
  this.playGuitar();  // calling parent mixin method
  
 }
});


// Example3:
     Ext.define('User', {
      constructor:function(){
       console.log('User constructor');
      },
      initComponent:function(){
       console.log('User initCom');
      },
      showUser: function() {
           console.log('showUser method called in User class');
          }
     });

     Ext.define('MadSkills', {
      constructor:function(){
       console.log('MadSkills constructor');
      },
      initComponent:function(){
       console.log('MadSkills initCom');
      },
         hackAway: function() {
          console.log('hackAway method called in Madskills class');
         }
     });
     
     Ext.define('Developer', {        
      extend: 'User',
      constructor:function(){
       console.log('Developer constructor');
      },
      initComponent:function(){
       console.log('Developer initCom');
      },
      showDeveloper: function() {
           console.log('showDeveloper method called in Developer class');
          },
         mixins: {
             madskills: 'MadSkills'
         },
         hackAway: function() {
          console.log('hackAway method called in Developer class');
          // delegate calling mixin of parent method
          this.mixins.madskills.hackAway.call(this);
         }
     });
     
     
Ext.application({
    name: 'Mixin',
    
    launch: function() {
        Ext.create('Ext.container.Viewport', {
            //layout: 'fit',
            items: [{
              xtype: 'panel',
              id: 'paneloneId',
              height : 100,
                    title: 'Mixin Example',
                    html:  'Mixin Example'
                },
                {
                 xtype: 'panel',
                 id: 'paneltwoId',
                 height : 100,
                    title: 'Mixin Example',
                    html:  'Mixin Example'
                   }
                   ]
        });
        this.callMusic();
        this.callDeveloper();
    },
    
    callMusic: function(){
     Ext.getCmp('paneloneId').setTitle('Music');
     
     /*
     Ext.require('Musician', function() {
            var mus = new Musician();
            mus.initComponent();
            mus.sing();
        }); */
     
     Ext.require('CoolPerson', function() {
      var mus = new CoolPerson();
      mus.initComponent();
      mus.sing(); // calling own class method
      mus.playGuitar(); //calling mixin parent method
     });
    },
    callDeveloper: function(){
     Ext.getCmp('paneltwoId').setTitle('Dev');
     Ext.require('Developer', function() {
            var dev = new Developer();
            dev.initComponent();
            dev.showDeveloper();
            dev.showUser();
            dev.hackAway();
        });
     
    }
});
// html code
output:
CoolPerson constructor
CoolPerson initCom
sing from coolperson...
sing method called in CanSing class...
playGuitar method called in CanPlayGuitar class...
playGuitar method called in CanPlayGuitar class...
Developer constructor
Developer initCom
showDeveloper method called in Developer class
showUser method called in User class
hackAway method called in Developer class
hackAway method called in Madskills class
Example 4: Dynamic Loading

1. Loading a single file

//This is the simplest class, used in example 1. This class does not depend on any others, so the example is run immediately
//after it is loaded
Ext.define('Product', {
    config: {
        name: 'Product Name'
    }
});

 
Ext.require('Product', function() {
                var product = new Product();
                product.setName('Ext JS 4');
                
                alert("Product name: " + product.getName());
            });

2. Loading a file with dependencies

//By defining properties in the config declaration we get functions for getEmail, setEmail,etc
//See for sample usage
Ext.define('User', {
    config: {
        email: '',
        isSuper: false
    }
});

//This class extends User, so User.js will be loaded first
Ext.define('SuperUser', {
    extend: 'User',
    
    //SuperUsers are always super...
    constructor: function() {
        this.setIsSuper(true);
    }
});

//Call
Ext.require('SuperUser', function() {
                var superUser = new SuperUser();
                superUser.setEmail('tom@sencha.com');
                
                alert("Is a SuperUser: " + superUser.isSuper());
            });

3. Mixins
//This class mixes in the MadSkills mixin (see MadSkills.js), and is used in example 3
Ext.define('Developer', {
    extend: 'User',
    mixins: {
        madskills: 'MadSkills'
    }
});

//This is a mixin, which we add to the class in Developer.js (see example 3)
Ext.define('MadSkills', {
    hackAway: function() {
        alert('I swear Abe, it worked 20 minutes ago');
    }
});

The MadSkills mixin contains a function called hackAway, which will be added to Developer:
            Ext.require('Developer', function() {
                var dev = new Developer();
                dev.setEmail('jerry@sencha.com');
                dev.hackAway();
            });

Finally,
Ext.define('LeetSkills', {
    makeItBetter: function() {
        alert("I don't care - I'm going to revert your commits!");
    }
});

//This is a fictional supporting class used by the Architect class to revert changes to a repository
Ext.define('CommitReverter', {
    config: {
        url: ''
    },
    
    revertCommits: function(count) {
        alert('Reverted ' + count + (count == 1 ? ' change' : ' changes'));
    }
});

Our final class extends the Developer class we used above, mixing in the LeetSkills mixin as well as the earlier MadSkills mixin, and requires the CommitReverter class, which is used by the revertCommits function at the end of the example.
            Ext.require('Architect', function() {
                var ed = new Architect();
                
                //from the MadSkills mixin
                ed.hackAway();
                
                //from the LeetSkills mixin
                ed.makeItBetter();
                
                //from the RevertChanges requirement
                ed.revertCommits(2);
            });
        

Comments

Post a Comment

Popular posts from this blog

ExtJS - Grid panel features

What can we do with ExtJS GridPanel? I have to develop a lot of applications in my web app and I see that grid component of ExtJS may fit in. However, I am not aware of what all things I can do with the - off the shelf available framework pieces - available plug-ins in the marketplace and - custom development through my own developers This is a typical question that we hear from the business users who wants to design an application by keeping the framework’s capability in perspective. In this article I have tried to put the list of stuff you can do with grid and hopefully that shall enable you to take advantage of the beauty of ExtJS. Pre-requisites This article assumes that you are familiar with basics of ExtJS What are the available options? In this section I will be taking you through some of the commonly seen usage of ExtJS grid panel. While covering all the capabilities of grid may not be possible, I am sure it will be helpful for the business users who want to...

ExtJS 4 with Spring MVC Example

Introduction When developing a brand new application for the company I work for, one of the first thing I implement is "authentication". Not only is this process generally a prerequisite to access many other functions but it is also very simple and covers every layer of an application, from GUI to the database. So instead of another minimalistic "Hello World" tutorial, here is a "Login/Password" example, using 2 famous technologies : Spring MVC and ExtJS 4.   Prerequisites Because this tutorial is quite long, I won't go into the details of each technology I used but I will mainly focus on how ExtJS and Spring can be nicely used together. So here are the things you should know before continuing : Spring framework (good knowledge) Spring MVC (basic knowledge. See  Spring MVC Official Documentation  for details) ExtJS "application architecture" (see  Sencha Docs  for details) Eclipse IDE + Tomcat 6 (or any other web container) You a...

Ext4 Apply Store Filtering

In extjs4.1: There are many way for store filtering . Some of code i give here Filtering by single field: store . filter ( 'eyeColor' , 'Brown' );   Alternatively, if filters are configured with an  id , then existing filters store may be  replaced by new filters having the same  id . Filtering by single field: store . filter ( "email" , /\.com$/ );   Using multiple filters: store . filter ([ { property : "email" , value : /\.com$/ }, { filterFn : function ( item ) { return item . get ( "age" ) > 10 ; }} ]);   Using  Ext.util.Filter  instances instead of config objects (note that we need to specify the root config option in this case): store . filter ([ Ext . create ( ' Ext.util.Filter ' , {   property : "email" , value : /\.com$/ , root : 'data' }),   Ext . create ( ' Ext.util.Filter ' , {   filterFn : function ( item ) {   return item . get ( ...