1
My.Awesome.Obj = function() {
    return {
        // PUBLIC PROPERTIES
        legendObj:  null,

        // PUBLIC METHODS
        init: function() {
            this.assignLegendClick();
        },

        assignLegendClick: function() {
            console.log('*** drawLegend()');
            var checkboxes = this.legendObj.find("input");
            checkboxes.each(function() {
                $(this).click(function() {
                    this.handleLegendClick();
                });
            })
        },

        handleLegendClick: function() {
            console.log('*** handleLegendClick()');
        },

        EOF: null
    };
}();

So I'm getting an error saying

handleLegendClick is not defined

I'm assuming this is a scope issue, but I'm not sure how to refer back to the parent object within the loop...

2 Answers 2

3

The problem is that this is the window when the click event handler is called.

You might do this :

My.Awesome.Obj = function() {
    var obj = {
        // PUBLIC PROPERTIES
        legendObj:  null,

        // PUBLIC METHODS
        init: function() {
            obj.assignLegendClick();
        },

        assignLegendClick: function() {
            console.log('*** drawLegend()');
            var checkboxes = obj.legendObj.find("input");
            checkboxes.each(function() {
                $(this).click(function() {
                    obj.handleLegendClick(); // <=== replace this with obj
                });
            })
        },

        handleLegendClick: function() {
            console.log('*** handleLegendClick()');
        },

        EOF: null
    };
    return obj;
}();
Sign up to request clarification or add additional context in comments.

3 Comments

not necessary to call obj.legendObj.find... in the assignLegendClick-function because at that point 'this' IS the object.
@hereandnow78 In fact it depends on how you call the function : if you copy the reference and call it without receiver, this ensures it's obj. But in most cases I agree it's useless, that's why I mentioned only the place where I've put a comment, which is the important one.
@destroy you were too fast, I still have to wait to be able to accept!
2

yes its a scoping issue right here:

assignLegendClick: function() {
  console.log('*** drawLegend()');
  var checkboxes = this.legendObj.find("input");
  checkboxes.each(function() {
    $(this).click(function() {
      this.handleLegendClick();
    });
  })

use a helper param to preserve the scope (usually called self or _self)

assignLegendClick: function() {
  console.log('*** drawLegend()');
  var self = this
    , checkboxes = this.legendObj.find("input");
  checkboxes.each(function() {
    $(this).click(function() {
      self.handleLegendClick();
    });
  })

Comments

Your Answer

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.