2

I want to call from Jquery scope (which is in the same class) to same class function/method. How I should do?

I tried:

displayTemplate.findWithAttr(action);

or

this.findWithAttr(action);

Response

"this.findWithAttr is not a function"

Here is my code.

class displayTemplate{
  findWithAttr(action) {
    //to do something with action
  }
  router(action){
    if(action == "something"){
      $( "#confirm_dialog_msg" ).text("text?");
         $( "#dialog-confirm" ).dialog({
           buttons: {
             "yes": function() {
                $( this ).dialog( "close" );

               //how in this area call to "findWithAttr" function above?
                this.findWithAttr(action);

             },
             "No": function() {
               //..
             }
           }
        });
      }
  //...
  }
}
parlad
  • 1,143
  • 4
  • 23
  • 42
evarc
  • 23
  • 4
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Heretic Monkey May 30 '19 at 13:07

2 Answers2

1

Before you enter the JQuery scope of the function, declare a variable like this

var self = this;

And then just do self.findWithAttr and that should work.

So like:

router(action){
if(action == "something"){
  var self = this;
  $( "#confirm_dialog_msg" ).text("text?");
     $( "#dialog-confirm" ).dialog({
       buttons: {
         "yes": function() {
            $( this ).dialog( "close" );

           //how in this area call to "findWithAttr" function above?
            self.findWithAttr(action);

         },
         "No": function() {
           //..
         }
       }
    });
  }

Hope this helped.

FeaturedSpace
  • 479
  • 3
  • 18
0

You may want to use lambda expressions instead of nested functions. One of the differences between them is that lambda expressions keep parent context instead of having their own.

So to do that you simply change

"yes": function() {

to

"yes": () => {