Skip to content

Instantly share code, notes, and snippets.

@stivenson
Forked from tobyzerner/app.js
Created May 13, 2017 06:30
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 stivenson/be284b1cab122c2ce4964ab0118ff552 to your computer and use it in GitHub Desktop.
Save stivenson/be284b1cab122c2ce4964ab0118ff552 to your computer and use it in GitHub Desktop.
Mithril ES6 Components
import Component from './component';
class Widget extends Component {
init(ctrl) {
var props = this.props;
ctrl.counter = props.initialValue;
ctrl.increment = function() {
ctrl.counter++;
}
ctrl.reset = function() {
ctrl.counter = props.initialValue;
}
}
view(ctrl) {
return m('div', [
m('h1', 'This widget is set up for: '+this.props.name),
m('p', 'Counter: '+ctrl.counter),
m('button', {onclick: ctrl.increment}, 'Increment Counter')
])
}
}
class IndexPage extends Component {
init(ctrl) {
ctrl.widget = new Widget({name: 'foo', initialValue: 2}).instance()
}
view(ctrl) {
return m('div', [
ctrl.widget.render(),
m('button', {onclick: ctrl.widget.reset}, 'Reset Counter')
]);
}
}
m.mount(document.body, new IndexPage());
export default class Component {
constructor(props) {
this.props = props || {};
var component = this;
this.controller = function() {
var ctrl = {};
component.init(ctrl);
return ctrl;
};
this.controller.$original = this.init;
}
init(ctrl) {
}
instance() {
var component = this;
var controller = new this.controller();
controller.render = function() {
return component.view(controller);
};
return controller;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment