1. #1
    Sencha User
    Join Date
    Dec 2007
    Posts
    9
    Vote Rating
    0
    mirws is on a distinguished road

      0  

    Question Answered: Ask about Mixins

    Answered: Ask about Mixins


    Hi...
    I have a question refer to this link http://docs.sencha.com/ext-js/4-0/api/Ext.Class

    In the mixins example,

    Code:
    ...
    
    Ext.define('CoolPerson', {
    	extend:'Person',
    	
    	mixins: {
    		canPlayGuitar:'CanPlayGuitar',
    		canSing:'CanSing'
    	},
    	
    	sing: function(){
    		alert("Ahem...");
    		this.mixins.canSing.sing.call(this);
    		this.playGuitar();
    		
    	}
    });
    ...
    What this line mean ?
    " this.mixins.canSing.sing.call(this); "

    Why we dont call sing() function directly like calling playGuitar(),

    Code:
    ...
    
    this.sing();
    this.playGuitar();
    ...
    why the code above not working? , but this is work

    Code:
    ...
    // this.sing();
    this.playGuitar();
    ...
    2nd question, when we should extend a class or mixins it, meanwhile both will add functionality to the class

  2. Quote Originally Posted by mirws View Post
    Hi...
    I have a question refer to this link http://docs.sencha.com/ext-js/4-0/api/Ext.Class

    In the mixins example,

    Code:
    ...
    
    Ext.define('CoolPerson', {
    	extend:'Person',
    	
    	mixins: {
    		canPlayGuitar:'CanPlayGuitar',
    		canSing:'CanSing'
    	},
    	
    	sing: function(){
    		alert("Ahem...");
    		this.mixins.canSing.sing.call(this);
    		this.playGuitar();
    		
    	}
    });
    ...
    What this line mean ?
    " this.mixins.canSing.sing.call(this); "

    Why we dont call sing() function directly like calling playGuitar(),

    Code:
    ...
    
    this.sing();
    this.playGuitar();
    ...
    why the code above not working? , but this is work

    Code:
    ...
    // this.sing();
    this.playGuitar();
    ...
    2nd question, when we should extend a class or mixins it, meanwhile both will add functionality to the class
    1. Because CoolPerson class has its own sing method, which overrides the one from the mixin. this.mixins.canSing refers to the prototype of the mixin, so that you can invoke the overridden method.

    2. Think of mixins as multiple inheritance whereby you can reuse code from multiple classes, as opposed to a single parent class.

  3. #2
    Sencha User
    Join Date
    Nov 2010
    Posts
    1
    Vote Rating
    0
    suplch is on a distinguished road

      0  

    Default


    ...

    Ext.define('CoolPerson', {
    extend:'Person',

    mixins: {
    canPlayGuitar:'CanPlayGuitar',
    canSing:'CanSing'
    },

    sing: function(){
    alert("Ahem...");
    this.mixins.canSing.sing.call(this);
    this.playGuitar();

    }
    });


    this.mixins.canSing.sing == this.sing //true
    this.mixins.canPlayGuitar.playGuitar== this.playGuitar//true

  4. #3
    Sencha - Sencha Touch Dev Team Jacky Nguyen's Avatar
    Join Date
    Jul 2009
    Location
    Palo Alto, California
    Posts
    469
    Vote Rating
    9
    Answers
    24
    Jacky Nguyen will become famous soon enough Jacky Nguyen will become famous soon enough

      0  

    Default


    Quote Originally Posted by mirws View Post
    Hi...
    I have a question refer to this link http://docs.sencha.com/ext-js/4-0/api/Ext.Class

    In the mixins example,

    Code:
    ...
    
    Ext.define('CoolPerson', {
    	extend:'Person',
    	
    	mixins: {
    		canPlayGuitar:'CanPlayGuitar',
    		canSing:'CanSing'
    	},
    	
    	sing: function(){
    		alert("Ahem...");
    		this.mixins.canSing.sing.call(this);
    		this.playGuitar();
    		
    	}
    });
    ...
    What this line mean ?
    " this.mixins.canSing.sing.call(this); "

    Why we dont call sing() function directly like calling playGuitar(),

    Code:
    ...
    
    this.sing();
    this.playGuitar();
    ...
    why the code above not working? , but this is work

    Code:
    ...
    // this.sing();
    this.playGuitar();
    ...
    2nd question, when we should extend a class or mixins it, meanwhile both will add functionality to the class
    1. Because CoolPerson class has its own sing method, which overrides the one from the mixin. this.mixins.canSing refers to the prototype of the mixin, so that you can invoke the overridden method.

    2. Think of mixins as multiple inheritance whereby you can reuse code from multiple classes, as opposed to a single parent class.
    Sencha Touch Lead Architect

  5. #4
    Sencha User
    Join Date
    Jun 2012
    Posts
    18
    Vote Rating
    0
    altanmur is on a distinguished road

      0  

    Default I have question.

    I have question.


    Code:
    Ext.define('Developer', {        extend: 'User',
    	constructor:function(){
    		alert('Developer contructor');
    	},
    	initComponent:function(){
    		alert('Developer initCom');
    	},
        mixins: {
            madskills: 'MadSkills'
        }
    })
    Code:
    Ext.define('User', {
    	constructor:function(){
    		alert('Developer contructor');
    	},
    	initComponent:function(){
    		alert('Developer initCom');
    	}
    });
    Code:
    Ext.define('MadSkills', {
    	contructor:function(){
    		alert('MadSkills contructor');
    	},
    	initComponent:function(){
    		alert('MadSkills initCom');
    	},
        hackAway: function() {
            alert('I swear Abe   , it worked 20 minutes ago');
        }
    });
    Code:
    Ext.require('Developer', function() {
                    var dev = new Developer();
                    dev.hackAway();
                });
    Why not working all construtor and initComponent?

Similar Threads

  1. Static mixins?
    By winkelmann in forum Ext: Discussion
    Replies: 6
    Last Post: 20 Apr 2011, 2:45 PM
  2. [OPEN-EXTJSIV-207] Mixins issues
    By LesJ in forum Ext:Bugs
    Replies: 3
    Last Post: 21 Mar 2011, 12:58 PM
  3. [INFOREQ]allow singleton as mixins
    By christophe.geiser in forum Ext:Bugs
    Replies: 4
    Last Post: 14 Mar 2011, 6:36 AM
  4. [CLOSED]Ext.AbstractComponent mixins
    By gevik in forum Ext:Bugs
    Replies: 5
    Last Post: 28 Feb 2011, 9:56 PM