ediblecode
ediblecode

Reputation: 11971

Getting this from javascript event listener in backbone.js

So for touch devices I have a couple of event listeners for touchstart and touchmove. Everything else has events bound by jQuery, but for some reason this didn't work for the touch events so they are bound using javascript:

document.addEventListener('touchstart', this.touchstart);
document.addEventListener('touchmove', this.touchmove);

The problem with this is that when I want to trigger an event from a jQuery bound event, I can just use, for example:

this.scrollContainer();

However, the context of this is different within the javascript bound event which means that I cannot trigger the event this way.

My question is, is it possible to trigger this event from the javascript bound event? If so, how?

Upvotes: 1

Views: 279

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388326

you need to use _.bind() to pass a custom context to the callback

document.addEventListener('touchstart', _.bind(this.touchstart, this));
document.addEventListener('touchmove', _.bind(this.touchmove, this));

Upvotes: 2

Related Questions