Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Last active December 17, 2015 16:39
Show Gist options
  • Save jonjaques/5639943 to your computer and use it in GitHub Desktop.
Save jonjaques/5639943 to your computer and use it in GitHub Desktop.
Super-simple touch event bindings for Knockout.js
// Uses Hammer-jquery | https://github.com/EightMedia/hammer.js/blob/master/dist/jquery.hammer.js
$.extend(ko.bindingHandlers, {
touchEvent: {
defaults: {},
init: function(el, valueAccessor, allBindingsAccessor) {
var self = ko.bindingHandlers.touchEvent,
events = ko.utils.unwrapObservable(valueAccessor()),
allOptions = allBindingsAccessor(),
opts = allOptions.touchEventOptions ? allOptions.touchEventOptions : {};
$.map(events, function(callback, event) {
var o = $.extend(self.defaults, opts);
$(el).hammer(o).on(event, callback);
ko.utils.domNodeDisposal.addDisposeCallback(el, function() {
$(el).off(event);
});
});
}
}
});
@jonjaques
Copy link
Author

Probably should write some update logic to handle unbinding callbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment