Skip to content

Instantly share code, notes, and snippets.

@dervalp
Created December 18, 2014 14:52
Show Gist options
  • Save dervalp/8a429127530f1dcc382a to your computer and use it in GitHub Desktop.
Save dervalp/8a429127530f1dcc382a to your computer and use it in GitHub Desktop.
Knockout + Hammer JS Binding
( function( window, ko ) {
var touchEvents = [ 'tap', 'doubletap', 'hold', 'rotate',
'drag', 'dragdown', 'dragup', 'dragleft', 'dragright',
'transform', 'transformstart',
'transformend', 'swipe', 'swipeleft', 'swiperight',
'swipeup', 'swipedown', 'pinch', 'pinchin', 'pinchout'
];
var makeMobileBindings = function( touchEventName ) {
ko.bindingHandlers[ touchEventName ] = {
init: function( element, valueAccessor, allBindingsAccessor, viewModel, bindingContext ) {
var handler = valueAccessor(),
allBindings = allBindingsAccessor();
Hammer( element ).on( touchEventName, function( e ) {
handler( viewModel, e );
} );
}
};
};
( function() {
for ( i in touchEvents ) {
var eventName = touchEvents[ i ];
makeMobileBindings( eventName );
}
} )();
}( this, ko ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment