-
-
Save darkone23/4315575 to your computer and use it in GitHub Desktop.
using a closure so middleware no longer requires currying
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
$.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