2

In my lightning component, I am actually extending a component , and trying to access its helper method, but fail to do so.

My super component has this method defined :

  closeModal : function(component) {
     ........               
  }

In my child component helper im trying to access the super helper like this :

  this.closeModal(component.getSuper());

I also tried this :

 component.getSuper().getHelper().closeModal(component.getSuper());

But nothing happens. So I think that it should be called another way, but how ? I didn't see any documentation on this. Only found this explanation on the following blog : http://peterknolle.com/lightning-component-inheritance/

"There is also a method to get the super component from the sub component context, component.getSuper()."

But they do not give any example.

3 Answers 3

4

If you are using component inheritance in lightning your component automatically inherits the methods of your parents helper. Just like any other case of inheritance your components helper will override methods with the same name on your super components helper. This inheritance happens through javascript prototypes.

For example: Component A (Super cmp) helper has a method X and a method Y

Component B (inherits A) helper has a method Y and a method Z

The helper of component B will in this case contain following methods Z, Y(with the implementation found on helper of B) and X (is inherited through the prototype)

To access the method X from your sub component simply call it on the helper of B.

So helperB.X().

Your this.closeModal() above should be correct, given of course that 'this' is infact your helper. You have to be carefull where you use this in your javascript code. It can be that for example a callback function is not actually executed from your helper object so 'this' no longer points to the helper.

Best advice I can give you here is to test the function directly from your controller and see if it reaches the function or not. If it does the issue might be related to 'this' not beign your helper.

For completeness the .getHelper method is not longer available to use. Here the list component functions that are allowed: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/cli_rules_secure_component.htm?search_text=getSuper

1

Only problem i had referring super's helper method was when i was using helper method inside some callback function.

if you call helper method inside of some callback (like action callback) - "this.closeModal" is not a recognized thing anymore ("this" in this context is not a helper, it is different object ), you have to explicitly bind it to callback using .bind(this)

function(){
  //some callbackcode here
}.bind(this); 

Hope this helps

1

Helpers are inherited, so from your 'child' component , just call

this.closeModal(component);

If you need to call it from an action callback, use code like this

var self  = this ;
action.setCallback(function() {
     self.closeModal(component);
});
$A.enqueueAction(action);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .