backbone.js中的移动手势

Dip*_*nan 6 events jquery-mobile backbone.js

我可以在Backbone.js查看事件中使用滑动,点按,捏等移动手势吗?更具体的是我的代码.

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

我可以使用滑动,向左/向右滑动,捏合,点按等移动手势来使用backbone.js吗?

Kev*_*zer 8

下载并包含Hammer.js然后像正常一样使用Backbone视图事件!

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
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以看看我简单的Backbone视图Gist

更新

根据反馈,看起来new Hammer(this.el)必须在骨干视图上调用才能工作.我已经更新了示例以反映这一点.

  • 为了实现这一点,我不得不在我的视图``el'上调用`hammer()`函数,就像我的View的`render()`方法中的`this.$ el.hammer()`一样.我从[这个答案](http://stackoverflow.com/a/16092066/877353)得到了这个,虽然我确实需要[jquery插件](http://hammerjs.github.io/jquery-plugin/)这个工作 (3认同)

fgu*_*len 0

Backbone依赖 jQuery.bind来管理 DOM 事件。

所以问题是,如果 jQuery 支持这些事件并且看起来像jQuery Mobile 一样,那么现在您必须检查如何集成 jQuery Mobile 和 Backbone