Skip to content

Instantly share code, notes, and snippets.

@gilbert
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbert/25b8aa6810b244665a2e to your computer and use it in GitHub Desktop.
Save gilbert/25b8aa6810b244665a2e to your computer and use it in GitHub Desktop.
Mithril Component Sugar
m.callableComponent = function (componentObj) {
var componentFn = function (props, content) {
return m.component(componentFn, props, content)
}
if (componentObj) {
for (var prop in componentObj) {
componentFn[prop] = componentObj
}
}
return componentFn
}
//
// Example use:
//
// [Option] Assignment style
MyWidget = m.callableComponent()
MyWidget.controller = function (options) { /*...*/ }
MyWidget.view = function (ctrl, options) { /*...*/ }
// [Option] Upfront style
MyWidget = m.callableComponent({
controller = function (options) { /*...*/ },
view = function (ctrl, options) { /*...*/ }
})
// Inclusion within another component's view:
m.module(document.body, {
view: function () {
return m('.app', [
m('h1', "Welcome!"),
MyWidget({ id: m.route.param('id') }) // <-----(here)
])
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment