Created
December 18, 2014 14:52
-
-
Save dervalp/8a429127530f1dcc382a to your computer and use it in GitHub Desktop.
Knockout + Hammer JS Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( 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