Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile gestures in backbone.js

Can I have mobile gestures like swipe, tap, pinch etc in the Backbone.js View events? To be more specific following is my code.

Backbone.View.extend({
     initialize:function(){
        //initialization 
     },
     Events:{
          "swipe-left #homeBtn":"homeSwipe"
     },
     homeSwipe:function(){
        alert("Event Swipe left triggered!");
     }
});

Can I have the mobile gestures like swipe, swipe-left/right, pinch, tap etc to work with backbone.js?

like image 363
Dipin Krishnan Avatar asked Apr 21 '12 19:04

Dipin Krishnan


1 Answers

Download and include Hammer.js and then use Backbone view events like normal!

events:{
    'swipe': 'onSwipe'
},

initialize: function(){
    // I think you can get away doing this here once, but I have not tested.
    // If not, just move it to the `render` method
    new Hammer(this.el);
},

onSwipe: function(e){
    console.log(e.direction); // left or right
}

Also, you could take a look at my simple Backbone view Gist

Update

Based on the feedback, it looks like new Hammer(this.el) must be called on the backbone view for this to work. I've updated the example to reflect this.

like image 119
Kevin Jantzer Avatar answered Nov 06 '22 08:11

Kevin Jantzer