Last active
October 9, 2018 09:17
-
-
Save alpap/8ce34c42df2586e0d12d270a931657de to your computer and use it in GitHub Desktop.
MIthrilJs Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="//unpkg.com/mithril/mithril.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.5.2/randomColor.min.js"></script> | |
<script> | |
var root = document.body | |
var count = 0 // added a variable | |
var Splash = { | |
view: function () { | |
return [m("a", { href: "#!/click" }, "1 click demo"), | |
m("br"), | |
m("a", { href: "#!/request" }, "2 request")] | |
} | |
} | |
var Click = { | |
view: function () { | |
return m("main", [ | |
m("a", { href: "#!/splash" }, "back"), | |
m("h1", { class: "title" }, "My first app"), | |
// changed the next line | |
m("h2", count), | |
m("button", { onclick: function () { count++ } }, " inctrement"), | |
m("button", { onclick: function () { count-- } }, " decrement"), | |
]) | |
} | |
} | |
var count2 = 0 | |
var jokes = [] | |
var getJoke = (name) => { | |
m.request({ | |
method: "GET", | |
url: "http://api.icndb.com/jokes/random?firstname=" + name, | |
data: {}, | |
withCredentials: false, | |
}) | |
.then(function (data) { | |
jokes.push(data.value.joke) | |
}) | |
} | |
// component | |
var jokesComponent = { | |
view: function (vnode) { | |
let jokelist = vnode.attrs.list | |
if (jokelist.length <= 0) return | |
let mjokes = [] | |
jokes.forEach((joke) => mjokes.push( | |
m("h4", { | |
style: "margin: 10px 5px 5px 5px; color:" + randomColor({ | |
luminosity: 'random', | |
hue: 'random' | |
}) | |
}, joke))) | |
return mjokes.reverse() | |
} | |
} | |
var Request = { | |
view: () => { | |
return m("main", [ | |
m("a", { href: "#!/splash" }, "back"), | |
m("h1", { class: "title" }, "My first app"), | |
// changed the next line | |
m("button", { onclick: getJoke }, "Get chuck joke"), | |
m(jokesComponent, { list: jokes }), | |
]) | |
} | |
} | |
m.route(root, "/splash", { | |
"/splash": Splash, | |
"/click": Click, | |
"/request": Request, | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment