Skip to content

Instantly share code, notes, and snippets.

@volnei
Last active January 26, 2018 10:44
Show Gist options
  • Save volnei/9a023abc77bc6c30931b8f86bc6f1c07 to your computer and use it in GitHub Desktop.
Save volnei/9a023abc77bc6c30931b8f86bc6f1c07 to your computer and use it in GitHub Desktop.
A mithril bind function
/**
* Use this function to bind input/select/textarea value (or other properties) to model.
* eg. m('input', {oninput: m.bind(myModel, 'property')})
* or
* eg. m('input[type=checkbox]', {onchange: m.bind('checked', myModel, 'property')}
*
* enjoy!
*/
m.bind = function(attr, obj, prop) {
if (arguments.length == 2) {
prop = obj
obj = attr
attr = 'value'
}
return m.withAttr(attr, function(value) {obj[prop] = value})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment