Skip to content

Instantly share code, notes, and snippets.

@darkone23
Forked from oojacoboo/utilities.js
Last active December 9, 2015 19:18
Show Gist options
  • Save darkone23/4315575 to your computer and use it in GitHub Desktop.
Save darkone23/4315575 to your computer and use it in GitHub Desktop.
using a closure so middleware no longer requires currying
$.fn.setupFilterInput = function (events) {
var $input = $(this),
handlers = {
// use these to provide default but overwriteable functionality
focus: function () {},
blur: function () {},
keyUp: function () {},
click: function () {}
},
middleware = {
// custom logic goes here
focus: function (handler, event) {
// conditionally use handler(event) to propogate to default or provided handler;
console.log("wow I did stuff before!")!
var result = handler(event);
console.log("and after!");
return result;
}
},
events = _(events).defaults(handlers);
_(events).each(function (event, handler) {
// if we find custom middleware for the handler use it
// otherwise use identity to return the unchanged handler
var wrap = (middleware[event] || _.identity);
$input.on(event, function(event) {
return wrap(handler, event);
});
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment