Mithril API

API

下面列出了最常用的方法。如果哪个方法在下面没有列出,则说明那个方法用于高级用法。

m(selector, attrs, children) - 文档

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

m.mount(element, component) - 文档

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) - 文档

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

m.route(document.body, "/home", {
    "/home": Home, // defines `http://localhost/#!/home`
})

m.route.set(path) - 文档

m.route.set("/home")

m.route.get() - 文档

var currentRoute = m.route.get()

m.route.prefix(prefix) - 文档

m.route() 之前调用。

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

m.request(options) - 文档

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

m.jsonp(options) - 文档

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

m.parseQueryString(querystring) - 文档

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

m.buildQueryString(object) - 文档

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

m.withAttr(attrName, callback) - 文档

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) - 文档

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

m.redraw() - 文档

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)