Skip to content

Instantly share code, notes, and snippets.

@michelmattos
Last active August 29, 2015 14:27
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 michelmattos/76b119fd4e5193660518 to your computer and use it in GitHub Desktop.
Save michelmattos/76b119fd4e5193660518 to your computer and use it in GitHub Desktop.
A "sugar" way to use Mithril's m.component.
'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);
}
}
'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