Setting context makes other helper methods unreachable #8

Closed
iros opened this Issue Nov 14, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Owner

iros commented Nov 14, 2012

If we set the context to another object, we can no longer reach the helper methods we define on the scene by just calling them on this.*

Perhaps somewhat related:

I really like being able to set context, and then retrieving it with this, but it doesn't always work, of course (JavaScript!):

context: $('#container'),
scenes : {
  init: {
    start: function() {
      console.log(this); // <- this is $('#container')
      var resize = function() {
        console.log(this); // <- this is window
      };
      $(window).resize(resize);
    }
  }
}

I guess I could use the that trick inside start, but at that point I might as well drop context altogether. Thoughts?

Owner

alexgraul commented Mar 15, 2013

When you're passing that function through as an event handler you'd have to bind it beforehand, ie:

context: $('#container'),
scenes : {
  init: {
    start: function() {
      console.log(this); // <- this is $('#container')
      var resize = _.bind(function() {
        console.log(this); // <- this is now $('#container')
      }, this);
      $(window).resize(resize);
    }
  }
}

Thanks for that - I just learned about apply and _.bind - neat.

@iros iros closed this May 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment