Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Setting context makes other helper methods unreachable #8

Closed
iros opened this Issue · 3 comments

3 participants

@iros
Owner

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.*

@gabrielflorit

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?

@alexgraul
Owner

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);
    }
  }
}
@gabrielflorit

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

@iros iros closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.