Andrea Coiutti
Front-end Developer. UX/UI Engineer. Webdesigner.
Andrea Coiutti
Codroipo, 18/07/16
Mithril is a client-side MVC framework.
A tool to organize code in a way that is easy to think about and to maintain.
Website | mithril.js.org |
Github | https://github.com/lhorie/mithril.js |
Wiki | https://github.com/lhorie/mithril.js/wiki |
Gitter | gitter.im/lhorie/mithril.js |
@mithriljs |
Created by Leo Horie
First release: ~ March 2014
current version: v0.2.5 - v1.0 rewrite in progress
AngularJS
Ember.js
jQuery
40kb
100kb
35kb
mithril
7kb
react
35kb
AngularJS
Ember.js
jQuery
200+
100+
200+
mithril
20
react
~ 50
???
CORE |
ROUTING | DATA |
---|---|---|
m | m.route | m.request |
m.prop | m.route.param | m.deferred |
m.component | m.sync | |
m.mount | ||
m.withAttr |
RENDERING | HTML | TESTING |
---|---|---|
m.render | m.trust | m.depts |
m.redraw | ||
m.startComputation | ||
m.endComputation |
AngularJS
Ember.js
Knockout
800ms
1000ms
300ms
mithril
80ms
react
600ms
http://matt-esch.github.io/mercury-perf/
The goal of the framework is to make application code discoverable, readable and maintainable, and hopefully help you become an even better developer.
Leo Horie
Nothing more. No crap you probably don't need.
https://github.com/lhorie/mithril.js/wiki/Who-Uses-Mithril
m('.container'); //<div class="container"></div>
m('#layout'); //<div id="layout"></div>
m('a[name=top]'); //<a name="top"></a>
m('[contenteditable]'); //<div contenteditable></div>
m('a#google.external[href="http://google.com"]', 'Google');
//<a id="google" class="external" href="http://google.com">Google</a>
m.mount(document.getElementById('content'), app);
var app = {};
// notice the lack of boilerplate
app.view = function(){
return m('h1', 'Hello World!');
};
// mount the application
m.mount(document.body, app);
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mithril Example</title>
</head>
<body>
<script src="mithril.js"></script>
<script src="hello-world.js"></script>
</body>
</html>
var app = {
controller: function() {
this.name = m.prop('');
},
view: function(ctrl) {
return [
m('h1', 'My name is: ' + ctrl.name()),
m('input', {
value: ctrl.name(),
oninput: m.withAttr('value', ctrl.name)
})
];
}
};
m.mount(document.body, app);
var app = {
getColors: function() {
return m.request({url: 'colors.json', method: 'GET', initialValue: []});
},
controller: function() {
this.colors = app.getColors();
},
view: function(ctrl) {
return m('ul', [
ctrl.colors().map(function(color) {
return m('li', 'Hex code for "' + color.name + '" is ' + color.value);
})
]);
}
};
m.mount(document.body, app);
var app = {
vm: {data: []},
controller: function() {
window.setInterval(function() {
for (var i = 0; i < 100; ++i) {
app.vm.data[i] = Math.random();
}
m.redraw();
}, 20);
},
view: function(ctrl) {
return m('table', [
m('thead', m('tr', [m('th', 'Key'), m('th', 'Value')])),
m('tbody', [
app.vm.data.map(function(value, index){
return m('tr', [m('td', index), m('td', value)]);
})
])
]);
}
};
m.mount(document.body, app);
var app = {
vm: {happy: false},
meHappy: function(framework) {
return m('figure', [
m('img[src="http://demo.acxwebdesign.com/mithril-examples/happy.jpg"]'),
m('h2', 'Me when I work with ' + framework)
]);
},
meNotHappy: function(framework) {
return m('figure', [
m('img[src="http://demo.acxwebdesign.com/mithril-examples/not-happy.jpg"]'),
m('h2', 'Me when I work with ' + framework)
]);
},
controller: function() {
window.setInterval(function() {
app.vm.happy = !app.vm.happy;
m.redraw();
}, 2000);
},
view: function(ctrl) {
return app.vm.happy ? app.meHappy('Mithril') : app.meNotHappy('Angular');
}
};
m.mount(document.body, app);
var app = {
drawCanvas: function(element, isInitialized) {
// executes this only once, avoids to repeat it at every redraw
if (isInitialized) return;
var ctx = element.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(0,0,150,75);
},
controller: function() { /* ... */ },
view: function(ctrl) {
return [
m('p', 'This is a red rectangle:'),
m('canvas', {config: app.drawCanvas})
];
}
};
m.mount(document.body, app);
By Andrea Coiutti
An introduction to Mithril.js for coworkers and friends. Thanks to Einar Norðfjörð, Bondi French and Stephan Hoyer for the heavy inspiration.