-
27 Apr 2011 7:42 PM #1
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,
What this line mean ?Code:... 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.call(this); "
Why we dont call sing() function directly like calling playGuitar(),
why the code above not working? , but this is workCode:... this.sing(); this.playGuitar(); ...
2nd question, when we should extend a class or mixins it, meanwhile both will add functionality to the classCode:... // this.sing(); this.playGuitar(); ...
-
Best Answer Posted by Jacky Nguyen
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.
-
27 Apr 2011 7:59 PM #2
...
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
-
30 Apr 2011 1:45 AM #3Sencha - Sencha Touch Dev Team
- Join Date
- Jul 2009
- Location
- Palo Alto, California
- Posts
- 469
- Vote Rating
- 9
- Answers
- 24
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
-
30 Aug 2012 10:53 AM #4
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'); } });Why not working all construtor and initComponent?Code:Ext.require('Developer', function() { var dev = new Developer(); dev.hackAway(); });
Similar Threads
-
Static mixins?
By winkelmann in forum Ext: DiscussionReplies: 6Last Post: 20 Apr 2011, 2:45 PM -
[OPEN-EXTJSIV-207] Mixins issues
By LesJ in forum Ext:BugsReplies: 3Last Post: 21 Mar 2011, 12:58 PM -
[INFOREQ]allow singleton as mixins
By christophe.geiser in forum Ext:BugsReplies: 4Last Post: 14 Mar 2011, 6:36 AM -
[CLOSED]Ext.AbstractComponent mixins
By gevik in forum Ext:BugsReplies: 5Last Post: 28 Feb 2011, 9:56 PM


Reply With Quote