Mithril 1.1.0

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 `http://localhost/#!/home`
})

m.route.set(path) - docs

m.route.set("/home")

m.route.get() - docs

var currentRoute = m.route.get()

m.route.prefix(prefix) - docs

Call this before m.route()

m.route.prefix("#!")
m("a[href='/Home']", {oncreate: m.route.link}, "Go to home page")

m.request(options) - docs

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

m.jsonp(options) - docs

m.jsonp({
    url: "/api/v1/users/:id",
    data: {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.withAttr(attrName, callback) - docs

var state = {
    value: "",
    setValue: function(v) {state.value = v}
}

var Component = {
    view: function() {
        return m("input", {
            oninput: m.withAttr("value", state.setValue),
            value: state.value,
        })
    }
}

m.mount(document.body, Component)

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.