2

This is probably pretty stupid but it's been bugging me for a little while now. Trying to get something going with AngularJS and this is killing me.

Within a callback I have something like so:

var xyz = {
  "modA"  : null,
  "modB"  : null,
  "modC"  : null
}

I then attach some methods as well...

xyz.setModA = function(elementId) {

  var someConfig = {};
  // attach some kind of event callback on stateChange
  someConfig.events.onStateChange = this.helper;

  this.modA = new someObject(someConfig);      

}

xyz.setModC = function(vars) {
    ....
}

xyz.helper = function(event) {
  ....
  this.setModC(vars);
}

When the state of the modA object we instantiated changes then the onStateChange event occurs and correctly fires the xyz.helper method I have but then I instantly get an Uncaught TypeError: undefined is not a function ? For some reason it's not seeing the setModC method now when the event fires?

3
  • 1
    Wouldn't this.setModC = 'something'; in xyz.helper blow away your setModC method? Commented May 30, 2014 at 17:02
  • @Cory Well seen. It's not current OP's bug but it could be the next one... Commented May 30, 2014 at 17:05
  • @Cory well seen indeed! Helper should've had this.setModC(cars); as the call which my real code actually has. Commented May 30, 2014 at 17:26

1 Answer 1

6

The context (this) in the event handler isn't the desired object.

To ensure the right context, change

someConfig.events.onStateChange = this.helper;

to

someConfig.events.onStateChange = this.helper.bind(this);
Sign up to request clarification or add additional context in comments.

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.