How to call a function of a parent class from another function in jquery

Mike

I guess that's the simple question. I'm new in js, especially in Backbone.js.

All I want to know is how I can refer to my function inside jquery function.

getLanguages: function() {

   ...

   return languages;
},

render: function() {

    ...

    $("input[type='checkbox']").bind("change", function() {

       // todo: getLanguages
    });

}

I tried to get languages via this but, of course, I got checkbox in this case.

Edit: It's so simple. Many thanks to all!!!

Buck Doyle

This is a classic problem in Javascript. You need to reference this inside a callback, but this changes to the element being bound to. A cheap way to do it:

render: function() {
    var that = this;

    $("input[type='checkbox']").bind("change", function() {
       that.getLanguages();
    });
}

that will stay defined as the this that render is defined on.

However, you’re using Backbone, and it has more suitable ways to handle this situation. I don’t know the name of your Backbone.View class, but here’s an example adapted from the documentation:

var DocumentView = Backbone.View.extend({

  events: {
    "change input[type='checkbox']": "doSomething"
  },

  doSomething: function() {
    this.getLanguages(); # uses the correct this
  }
});

Calling bind inside render is not The Backbone Way. Backbone views are made to handle event delegation without the unfortunate need to pass this around.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to call a function from another function in Jquery

From Java

How to call a parent class function from derived class function?

From Dev

How to call parent class function from child class function in Flutter

From Dev

How to call a function from another class in python?

From Dev

How to call function from another class anywhere

From Dev

PHP how to call function from another class?

From Dev

How to call a function from another class in python

From Dev

How to call function from another class?

From Dev

How to call a function from another class

From Dev

how to call a variable from one class function to another class function

From Dev

jquery how to call datepicker from another function?

From Dev

Call a function from an another class

From Dev

How do I call a function that is member of a class, from another function?

From Dev

How to call function in another class

From Dev

PHP How to call a function from another class and another file?

From Dev

Call function from another function in jQuery OOP

From Dev

How to call function from one class from another?

From Dev

PHP - Call class function from another class

From Dev

oop call parent function from child class

From Dev

Using jquery how to call a function and call another function from the previous when function execution is completed

From Dev

How to call another Codeigniter Class/function from other Codeigniter

From Dev

How to call one function(method) from another in a class?

From Dev

How to call a number from another function into a function?

From Dev

How to call from function to another function

From Dev

How to call a function from within another function?

From Dev

How to call a function of child class inside a function of parent class

From Dev

PHP - Call function from after call to another function in the class

From Dev

Execute function to call performSegueWithIdentifier from another class

From Dev

Execute function to call performSegueWithIdentifier from another class

Related Related

  1. 1

    how to call a function from another function in Jquery

  2. 2

    How to call a parent class function from derived class function?

  3. 3

    How to call parent class function from child class function in Flutter

  4. 4

    How to call a function from another class in python?

  5. 5

    How to call function from another class anywhere

  6. 6

    PHP how to call function from another class?

  7. 7

    How to call a function from another class in python

  8. 8

    How to call function from another class?

  9. 9

    How to call a function from another class

  10. 10

    how to call a variable from one class function to another class function

  11. 11

    jquery how to call datepicker from another function?

  12. 12

    Call a function from an another class

  13. 13

    How do I call a function that is member of a class, from another function?

  14. 14

    How to call function in another class

  15. 15

    PHP How to call a function from another class and another file?

  16. 16

    Call function from another function in jQuery OOP

  17. 17

    How to call function from one class from another?

  18. 18

    PHP - Call class function from another class

  19. 19

    oop call parent function from child class

  20. 20

    Using jquery how to call a function and call another function from the previous when function execution is completed

  21. 21

    How to call another Codeigniter Class/function from other Codeigniter

  22. 22

    How to call one function(method) from another in a class?

  23. 23

    How to call a number from another function into a function?

  24. 24

    How to call from function to another function

  25. 25

    How to call a function from within another function?

  26. 26

    How to call a function of child class inside a function of parent class

  27. 27

    PHP - Call function from after call to another function in the class

  28. 28

    Execute function to call performSegueWithIdentifier from another class

  29. 29

    Execute function to call performSegueWithIdentifier from another class

HotTag

Archive