Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created May 6, 2016 10:40
Show Gist options
  • Save barneycarroll/f58f1be86ad75f09775c206db2bccbdd to your computer and use it in GitHub Desktop.
Save barneycarroll/f58f1be86ad75f09775c206db2bccbdd to your computer and use it in GitHub Desktop.
Bind custom events on Mithril elements via config function
export default ( keyOrHash, handler ) =>
function config( el, init, ctxt ){
if( init ) return
const hash = handler ? {
[ keyOrHash ] : handler
} : keyOrHash
for( let key in hash )
if( hash.hasOwnProperty( key ) )
el.addEventListener( key, hash )
ctxt.onunload = () => {
for( let key in hash )
if( hash.hasOwnProperty( key ) )
el.removeEventListener( key, hash )
}
}
@barneycarroll
Copy link
Author

barneycarroll commented May 6, 2016

import on      from 'mithril-on'
import multi   from 'multi' // https://gist.github.com/barneycarroll/6671463fbe593b2bd8a8
import pluginX from 'wherever'

m( 'element', {
  config : on( 'click', e => e.preventDefault() )
} )

m( 'element', {
  config : on( {
    'click' : e => e.preventDefault(), 
    'focus' : e => e.target.blur()
  } )
} )

m( 'element', {
  config : multi(
    pluginX,
    on( {
      'click' : e => e.preventDefault(), 
      'focus' : e => e.target.blur()
    } )
} )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment