Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Last active February 4, 2016 15:20
Show Gist options
  • Save hajimehoshi/d8d74552d05cdf89b882 to your computer and use it in GitHub Desktop.
Save hajimehoshi/d8d74552d05cdf89b882 to your computer and use it in GitHub Desktop.
Mithril Tutorial in a more ES6 way
'use strict';
// http://mithril.js.org/getting-started.html in a more ES6 way.
class Todo {
constructor(data) {
this.description_ = data.description;
this.done_ = false;
}
get description() { return this.description_; }
get done() { return this.done_; }
set done(done) { this.done_ = done; }
}
class VM {
constructor() {
this.list = [];
this.description_ = ''
}
get description() { return this.description_; }
set description(description) { return this.description_ = description; }
add() {
if (!this.description_)
return;
this.list.push(new Todo({description: this.description_}));
this.description = '';
}
}
class Controller {
constructor() {
this.vm = new VM();
}
}
let component = (() => {
var comp = {};
comp.controller = Controller;
comp.view = (ctrl) => {
let vm = ctrl.vm;
return [
m('input', {onchange: (e) => { vm.description = e.target.value; },
value: vm.description}),
m('button', {onclick: vm.add.bind(vm)}, 'Add'),
m('table', [
vm.list.map((task, index) => {
return m('tr', [
m('td', [
m('input[type=checkbox]',
{onclick: (e) => {task.done = e.target.checked},
checked: task.done})
]),
m('td',
{style: {textDecoration: task.done ? 'line-through' : 'none'}},
task.description),
])
})
])
];
};
return comp;
})();
document.addEventListener('DOMContentLoaded', () => {
m.mount(document.body, component);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment