evarc
evarc

Reputation: 23

from jquery scope call to class function

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() {
               //..
             }
           }
        });
      }
  //...
  }
}

Upvotes: 2

Views: 50

Answers (2)

Farkhad Gojazadeh
Farkhad Gojazadeh

Reputation: 96

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": () => {

Upvotes: 0

FeaturedSpace
FeaturedSpace

Reputation: 499

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.

Upvotes: 1

Related Questions