mithril-vndb/docs/api.md
Isiah Meadows 234b1c9302 Update migration, fix various minor issues
- Lot of people couldn't migrate to v1 and plan to reevaluate when v2 is
  released.
- It's "npm" not "NPM". It doesn't stand for anything, and it never
  has - it was initially chosen simply because it was easy to type.
  It has a lot of unofficial backronyms with "Node Package Manager"
  being one of the most common ones, but it's never officially stood
  for anything as an acronym *or* initialism.
- Fixed a few errors in the change log, like non-breaking changes being
  included in the "Breaking Changes" section and an inaccuracy in the
  summary of a particular change.
- Fixed RawGit URLs to point to GitHack, which is a lighter proxy that
  offloads caching to Cloudflare instead of also implementing it itself.
  (It also just uses nginx for all the important server logic, so it
  scales better.)
- Add a few more v0.2 references as appropriate
2019-07-24 05:01:20 -04:00

2.3 KiB

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, {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)