首先,可以让Hammer与Backbone完美地配合使用。
为了达到这个目标,您只需要遵循以下步骤:
Create a Hammer Manager instance so you can listen for Hammer events. You should initiate the instance on the same element you are triggering the touch events, or an enclosing one (typically, your view element).
Thus something like this should be present at the end of your View's render()
function :
this.$el.hammer(); // Instantiates Hammer Manager
Listen for an event, as you normally would for any javascript/jQuery event :
events : {
"pan #some_selector": "some_handler",
}
In your event handler, you can access the event's properties within the gesture
attribute, not directly as written in Hammer's documentation, as follows :
function some_handler : function(event) {
console.log(event.gesture.deltaX);
},
使用Require、Backbone和Hammer有些困难,我花了一些时间才搞定。以下是我的解决方案。
Require配置:
require.config({
paths: {
"jquery": "/bower_components/jquery/jquery",
"underscore": "/bower_components/underscore/underscore",
"backbone": "/bower_components/backbone/backbone",
"hammerjs": "/bower_components/hammerjs/hammer",
"jquery-hammerjs": "/bower_components/jquery-hammerjs/jquery.hammer"
},
shim: {
"backbone": {
deps: [ "jquery-hammerjs", "underscore" ],
exports: "Backbone"
},
"underscore": {
exports: "_"
}
}
});
查看文件
define( "myview", [ "backbone" ], function( Backbone ) {
"use strict";
return Backbone.View.extend( {
el: ".my-el",
events: {
"tap .child-of-my-el": "handleTap"
},
initialize: function() {
this.$el.hammer( { domEvents: true } );
}
} );
} );