3

I have a simple jQuery ready event that initializes a view by calling the a function in the setupView object.

The question I have is, what is appropriate way to call the function setSomethingImportant from the init function as shown below?

Since the call is made from a different execution context than the init function, this.setSomethingImportant() does not work. However it works if I use setupView.setSomethingImportant(). The problem I have with this is that if the var name (setupView) changes, I will have to change the body of the code as well.

 (function() { 
 $(document).ready(function() { 
 setupView.init(); 
 }); 
 var setupView = { 
 currentState : "CT", 
 init : function () {
 $("#externalProtocol").change( function () {
 console.log("Changed =" + $(this).val());
 setSomethingImportant(); 
 // Question ? how to call a method in the setupView object 
 }); 
 }, 
 setSomethingImportant : function () {
 this.currentState="TC"; 
 console.log("Something has changed :" + this.currentState );
 } 
 } 
 }(jQuery);
asked Aug 9, 2012 at 16:01

3 Answers 3

4

Store this into a variable:

var setupView = {
 currentState: "CT",
 init: function() {
 // Keep a reference to 'this'
 var self = this;
 $("#externalProtocol").change(function() {
 console.log("Changed =" + $(this).val());
 // Use the old 'this'
 self.setSomethingImportant();
 });
 },
 setSomethingImportant: function() {
 this.currentState = "TC";
 console.log("Something has changed :" + this.currentState);
 }
};

See the Working demo.

answered Aug 9, 2012 at 16:09

2 Comments

Thanks @Florent. To be honest I have been using the approach you suggested. But I was not sure if storing "this" in a variable is a best practice.
Sometimes you don't have the choice. The less you use this trick, the cleaner your code will be.
1

Just declare the function separately and then call like so:

function setSomethingImportant(context) {
 context.currentState="TC"; 
 console.log("Something has changed :" + context.currentState );
};
(function() { 
 $(document).ready(function() { 
 setupView.init(); 
 }); 
 var setupView = { 
 currentState : "CT", 
 init : function () {
 $("#externalProtocol").change( function () {
 console.log("Changed =" + $(this).val());
 setSomethingImportant(this); 
 // Question ? how to call a method in the setupView object 
 }); 
 }, 
 setSomethingImportant : function () {
 setSomethingImportant(this);
 } 
 } 
}(jQuery);
answered Aug 9, 2012 at 16:09

1 Comment

You're still passing the wrong context from within the change handler.
1

Please note that I changed my original solution. I am now passing data to the event handler using even.data.

(function() { 
 $(document).ready(function() { 
 setupView.init(); 
 }); 
 var setupView = { 
 currentState : "CT", 
 init : function () {
 $("#externalProtocol").change({ _this: this }, function (event) {
 console.log("Changed =" + $(this).val());
 event.data._this.setSomethingImportant(); 
 }); 
 }, 
 setSomethingImportant : function () {
 this.currentState="TC"; 
 console.log("Something has changed :" + this.currentState );
 } 
 } 
 }(jQuery);
answered Aug 9, 2012 at 16:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.