1

As we all know helper methods in Lightning usually have the following signature:

openAffiliationCreateDialog : function(component, event) { ... }

My question here, is it possible to stop passing component around and figure it out from current context (i.e. THIS) right inside helper function?

We have some hierarchy of components which share common logic from basic helper through inheritance. And not all of basic helper methods have component as param. Now we need to add some additional functionality inside those methods "without component" which need to access some component attributes. So I'm wondering if there is a better way instead of going and modifying each helper method execution passing component as a param.

  • I wonder if you could declare it as an attribute and set it using a set attribute ie <aura:attribute name="cmp" type="Object" /> - <aura:set value="{!this}" attribute="cmp"/> – Caspar Harmer Jul 17 '17 at 8:33
0

I did a wee test and you CAN do this:

Set the component on init:

Component:

<aura:handler name="init" value="{!this}" action="{!c.init}" />

Handler:

thisCmp:{},

init:function(component) {
   thisCmp = component;
   this.getData(component);
},


getData:function() {
  var testCmp = thisCmp;
  ...etc
  //this is a valid component.
}

I'm not sure how long this component will remain valid for... Might be a good idea to get the Lightning Team to weigh on on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.