Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created August 17, 2014 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/0cd249cf43941711253f to your computer and use it in GitHub Desktop.
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.
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