Last active
December 17, 2015 16:39
-
-
Save jonjaques/5639943 to your computer and use it in GitHub Desktop.
Super-simple touch event bindings for Knockout.js
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
// 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); | |
}); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Probably should write some update logic to handle unbinding callbacks.