Handling touch events in a Backbone View

1.8k Views Asked by At

I want my backbone app to behave nicely on mobile devices. Does it makes sense to replace 'click' events with 'touchend' events?

var CoolView = Backbone.View.extend({
    el : $("#coolEl"),
    events: {
        'touchend' : 'clickHandler'
    }
});

add it to the events object?

var CoolView = Backbone.View.extend({
    el : $("#coolEl"),
    events: {
        'click' : 'clickHandler',
        'touchend' : 'clickHandler'
    }
});

something else?

fiddle

2

There are 2 best solutions below

1
On BEST ANSWER

You can use the Backbone.touch plugin which will replace the click events to touch events if the device is mobile.

0
On

One potential solution is this: https://stackoverflow.com/a/7828579/1742747

Which is to detect

navigator.userAgent.match(/mobile/i)

and make a determination about which listener to apply based on that.

fiddle: http://jsfiddle.net/dira/Ke2px/2/