Last active
August 29, 2015 14:27
-
-
Save michelmattos/76b119fd4e5193660518 to your computer and use it in GitHub Desktop.
A "sugar" way to use Mithril's m.component.
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
'use strict'; | |
var m = require('mithril'); | |
function isObject(object) { | |
return {}.toString.call(object) === "[object Object]"; | |
} | |
module.exports = function(component) { | |
return function() { | |
var args = [].slice.call(arguments), | |
hasAttrs = args[0] != null && isObject(args[0]) && !("tag" in args[0] || "view" in args[0] || "subtree" in args[0]), | |
attrs = hasAttrs ? args[0] : {}, | |
children = !hasAttrs ? args[0] : args[1]; | |
return m.component.call(this, component, attrs, children); | |
} | |
} |
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
'use strict'; | |
var m = require('mithril'), | |
cmp = require('./componentization.js'); | |
var hello = cmp({ | |
controller: function(attrs, name) { | |
this.hello = 'Hello '; | |
}, | |
view: function(ctrl, attrs, name) { | |
return m('div', attrs, ctrl.hello + name + '!'); | |
} | |
}); | |
m.render(document.body, | |
m('div', [ | |
hello('Bilbo Baggins'), | |
hello({ onclick: function() { console.log('My precious!!!'); } }, 'Gandalf') | |
]) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment