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

151 lines
2.3 KiB
Markdown

# 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](hyperscript.md)
```javascript
m("div.class#id", {title: "title"}, ["children"])
```
---
#### m.mount(element, component) - [docs](mount.md)
```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](route.md)
```javascript
var Home = {
view: function() {
return "Welcome"
}
}
m.route(document.body, "/home", {
"/home": Home, // defines `https://example.com/#!/home`
})
```
#### m.route.set(path) - [docs](route.md#mrouteset)
```javascript
m.route.set("/home")
```
#### m.route.get() - [docs](route.md#mrouteget)
```javascript
var currentRoute = m.route.get()
```
#### m.route.prefix = prefix - [docs](route.md#mrouteprefix)
Invoke this before `m.route()` to change the routing prefix.
```javascript
m.route.prefix = "#!"
```
#### m(m.route.Link, ...) - [docs](route.md#mroutelink)
```javascript
m(m.route.Link, {href: "/Home"}, "Go to home page")
```
---
#### m.request(options) - [docs](request.md)
```javascript
m.request({
method: "PUT",
url: "/api/v1/users/:id",
params: {id: 1, name: "test"}
})
.then(function(result) {
console.log(result)
})
```
---
#### m.jsonp(options) - [docs](jsonp.md)
```javascript
m.jsonp({
url: "/api/v1/users/:id",
params: {id: 1},
callbackKey: "callback",
})
.then(function(result) {
console.log(result)
})
```
---
#### m.parseQueryString(querystring) - [docs](parseQueryString.md)
```javascript
var object = m.parseQueryString("a=1&b=2")
// {a: "1", b: "2"}
```
---
#### m.buildQueryString(object) - [docs](buildQueryString.md)
```javascript
var querystring = m.buildQueryString({a: "1", b: "2"})
// "a=1&b=2"
```
---
#### m.trust(htmlString) - [docs](trust.md)
```javascript
m.render(document.body, m.trust("<h1>Hello</h1>"))
```
---
#### m.redraw() - [docs](redraw.md)
```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)
```