Created
August 17, 2014 21:10
-
-
Save barneycarroll/0cd249cf43941711253f to your computer and use it in GitHub Desktop.
I thought this was the most useful thing ever devised for DOM event handling. Then I realised this is just generic a generic function composition tool geared to an over-specified use case.
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
var _ = require( 'lodash' ); | |
var m = require( 'mithril' ); | |
_.extend( m, { | |
// Takes any number of transforms, returns a function wrapper: | |
// When executed, function will be passed through each transform | |
// sequentially before its own body is executed. | |
// | |
// As a DOM event handler: | |
// onclick: m.transform( m.prevent, m.stop )( x ) | |
// x is the custom event handler, but prevent & stop run first | |
transform : function transformWrapper(){ | |
var transforms = _.toArray( arguments ); | |
return function functionWrapper( handler ){ | |
return function transform(){ | |
var ctxt = this; | |
var args = _.toArray( arguments ); | |
_.each( transforms, function applyTransform( transform ){ | |
transform.apply( ctxt, args ); | |
} ); | |
return handler.apply( ctxt, args ); | |
}; | |
}; | |
}, | |
prevent : function preventDefault( event ){ | |
event.preventDefault(); | |
}, | |
stop : function stopPropagation( event ){ | |
event.preventDefault(); | |
}, | |
kill : function returnFalse( event ){ | |
return false; | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment