Mobile gestures in backbone.js

浪尽此生 提交于 2019-12-31 18:02:23

问题


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?


回答1:


Backbone relies on jQuery.bind to manage the DOM events.

So the question is that if jQuery supports these events and looks like jQuery Mobile does, now you have to check how to integrate jQuery Mobile and Backbone.




回答2:


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.



来源:https://stackoverflow.com/questions/10262201/mobile-gestures-in-backbone-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!