1

In backbone js I used to bind 'this' to function, something like this,

this.someFunction = this.someFunction.bind(this);

I would like to implement something similar in Salesforce helper function. Is this possible?

EDIT: ComponentController.js

someFunction: function() {
    helper.openAPanel(component);
}

ComponentHelper.js

openAPanel: function(component) {
    //Here I will be requesting a certain resource from external website
    // On getting the resource i will be doing the certain things and one of them is having an onclick listener which handles toggling of tabs in that panel
    $('.tabs').on('click', this.toggleTab);
},

toggleTab: function(event) {
   if(event.currentTarget == 'panel1') {
      this.showPanel1();
   } else {
      this.showPanel2();
   }
},

showPanel1: function(){
    //handling
},

showPanel2: function(){
    //handling
}

But I get the error showPanel1 or showPanel2 is undefined. And when I print out 'this' it returns Proxy {}

5
  • In java script side you can call a method like this: this.method1(); suppose if that method in helper you have to call helper.method1(); May 11 '18 at 11:12
  • There are 3 functions in helper, I call the first function and on getting the result i call the second function (this.secondFuntion) and some execution takes place and in turn tries to call the third function (this.thirdFunction) and it fails here since 'this' here is undefined
    – Anto Joy
    May 11 '18 at 11:20
  • share your code snippets..I will tell you what you have to do May 11 '18 at 11:22
  • Can you post your JS functions to be able to see the issue and provide some direction?
    – Jayant Das
    May 11 '18 at 12:21
  • I would heavily advise against this. Helpers are shared among all the instances of your component. Binding the this property to the helper, would mean only one instance of your component would have the right context on that method.
    – Kris Gray
    May 15 '18 at 20:42
0

As per your comment I believe that you are looking for below answer:

In javascript side.

   testmethod:function(Component,event,helper){
   this.testmethoddetails(Component,event,helper);
 }

In helper:

 testmethoddetails:function(Component,event,helper){
    helper.method1();
}
 method1:function(Component,event,helper){
   helper.method2();
  }
  method2:function(Component,event,helper){
  helper.method3(Component,event,helper)
      }     
  method3:function(Component,event,helper){
 }
0

I generally used helper.methodName but if you do not want to use it then you can declare this on top on your function like.

function1 : function function1(component){
    var self = this;
}

Then you can use self wherever you were using this. (self.secondFuntion) and some execution takes place and in turn tries to call the third function (self.thirdFunction)

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.