API
Cheatsheet
Here are examples for the most commonly used methods. If a method is not listed below, it's meant for advanced usage.
m(selector, attrs, children) - docs
javascript
m('div.class#id', { title: 'title' }, ['children']);
m.mount(element, component) - docs
javascript
var state = {
count: 0,
inc: function () {
state.count++;
},
};
var Counter = {
view: function () {
return m('div', { onclick: state.inc }, state.count);
},
};
m.mount(document.body, Counter);
m.route(root, defaultRoute, routes) - docs
javascript
var Home = {
view: function () {
return 'Welcome';
},
};
m.route(document.body, '/home', {
'/home': Home, // defines `https://example.com/#!/home`
});
m.route.set(path) - docs
javascript
m.route.set('/home');
m.route.get() - docs
javascript
var currentRoute = m.route.get();
m.route.prefix = prefix - docs
Invoke this before m.route()
to change the routing prefix.
javascript
m.route.prefix = '#!';
m(m.route.Link, ...) - docs
javascript
m(m.route.Link, { href: '/Home' }, 'Go to home page');
m.request(options) - docs
javascript
m.request({
method: 'PUT',
url: '/api/v1/users/:id',
params: { id: 1, name: 'test' },
}).then(function (result) {
console.log(result);
});
m.parseQueryString(querystring) - docs
javascript
var object = m.parseQueryString('a=1&b=2');
// {a: "1", b: "2"}
m.buildQueryString(object) - docs
javascript
var querystring = m.buildQueryString({ a: '1', b: '2' });
// "a=1&b=2"
m.trust(htmlString) - docs
javascript
m.render(document.body, m.trust('<h1>Hello</h1>'));
m.redraw() - docs
javascript
var count = 0;
function inc() {
setInterval(function () {
count++;
m.redraw();
}, 1000);
}
var Counter = {
oninit: inc,
view: function () {
return m('div', count);
},
};
m.mount(document.body, Counter);