Mithril 2.0.3

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

m("div.class#id", {title: "title"}, ["children"])

m.mount(element, component) - docs

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

var Home = {
    view: function() {
        return "Welcome"
    }
}

m.route(document.body, "/home", {
    "/home": Home, // defines `https://example.com/#!/home`
})

m.route.set(path) - docs

m.route.set("/home")

m.route.get() - docs

var currentRoute = m.route.get()

m.route.prefix = prefix - docs

Invoke this before m.route() to change the routing prefix.

m.route.prefix = "#!"

m(m.route.Link, ...) - docs

m(m.route.Link, {href: "/Home"}, "Go to home page")

m.request(options) - docs

m.request({
    method: "PUT",
    url: "/api/v1/users/:id",
    params: {id: 1, name: "test"}
})
.then(function(result) {
    console.log(result)
})

m.jsonp(options) - docs

m.jsonp({
    url: "/api/v1/users/:id",
    params: {id: 1},
    callbackKey: "callback",
})
.then(function(result) {
    console.log(result)
})

m.parseQueryString(querystring) - docs

var object = m.parseQueryString("a=1&b=2")
// {a: "1", b: "2"}

m.buildQueryString(object) - docs

var querystring = m.buildQueryString({a: "1", b: "2"})
// "a=1&b=2"

m.trust(htmlString) - docs

m.render(document.body, m.trust("<h1>Hello</h1>"))

m.redraw() - docs

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)

License: MIT. © Leo Horie.