How to get this .grandparent () function in Mootools?

I built a class in Mootools and extended it twice, so there is a grandparent, parent, and child relationship:

var SomeClass1 = new Class({ initialize: function() { // Code }, doSomething: function() { // Code } }); var SomeClass2 = new Class({ Extends: SomeClass1, initialize: function() { this.parent(); }, doSomething: function() { this.parent(); // Some code I don't want to run from Class3 } }); var SomeClass3 = new Class({ Extends: SomeClass2, initialize: function() { this.parent(); }, doSomething: function() { this.grandParent(); } }); 

From Class3 , a child, I need to call the doSomething() method from Class1 , grandparent, without executing any code in Class2#doSomething() , the parent.

I need the grandParent() method to complement Mootools parent() , but it doesn't seem to exist.

What is the best way to accomplish this in Mootools or in pure JavaScript? Thanks.

UPDATE:

I should have mentioned: I realized that poor design makes me ask this question first. Mixing would be ideal, but I inherited the code and did not have time to refactor at the moment.

+4
source share
4 answers

this probably won't work for you, but ... if you add SomeClass1 as a mix and remove the local doSomething definition from SomeClass3 , then calling the doSomething method on the instance will call SomeClass1.doSomething(); direct.

this may not be practical if your doSomething on SomeClass3 should run local / separate code, but you can work around it, perhaps.

http://www.jsfiddle.net/29MGa/1/

There should be a way to get to the root of the inheritance chain from the nth level, but I cannot help you. you should go to the mootools mailing list and post this in the hope that someone from the main team will respond (e.g. ryan florence, aaron newton, christoph pojer, etc.). Another good source is the mootools IRC # mootools channel on irc.freenode.net.

Good luck, please update this with your findings, as you never know when this might be needed.

update from irc:

<akaIDIOT> SomeClass1.prototype.doSomething.apply(this[, ...]);

<akaIDIOT> not as clean as .parent(), but Moo doesn't give you a grandparent :)

also mixin gets a thumbs up:

<rpflo> d_mitar: I've often found that if I'm trying to do that it might make more sense for class 2 or 3 to be a mixin

<rpflo> but yeah, akaIDIOT should work

+1
source

As mentioned earlier, I'm sure using mixin makes more sense here, but here you go.

http://jsfiddle.net/rpflorence/24XJN/

 var GrandParent = new Class({ initialize: function(){ console.log('init:GrandParent'); }, talk: function(){ console.log('talk:GrandParent'); } }); var Parent = new Class({ Extends: GrandParent, initialize: function(){ this.parent(); console.log('init:Parent'); }, talk: function(){ console.log('talk:Parent'); } }); var Child = new Class({ Extends: Parent, initialize: function(){ this.parent(); console.log('init:Child'); }, talk: function(){ GrandParent.prototype.talk.apply(this); console.log('talk:Child'); } }); 
+2
source

I do not have mootools to check, but ...

You tried

 (this.parent()).parent(); 

?

0
source

Could you just name him in the grandparent class?

 SomeClass1.doSomething.apply(this,arguments); 

Or maybe even:

 SomeClass1.prototype.doSomething.apply(this, arguments); 

I am not 100% sure how the MooTools classes work, but one of these suggestions should work.

Also, if you have functionality in doSomething() inside SomeClass2 that you don't want to inherit for SomeClass3, why is SomeClass2 parent? You should be able to make another class a parent class, which includes the functions needed for both SomeClass2 and SomeClass3, and then allow everyone to override the doSomething() method in their own way.

0
source

Source: https://habr.com/ru/post/1311886/


All Articles