Modified mithril.html
Added controller for API requests
This commit is contained in:
parent
35d70443be
commit
7c61498047
32
App/Controllers/Api.php
Normal file
32
App/Controllers/Api.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace App\Controllers;
|
||||
use Core\Token;
|
||||
|
||||
class Api
|
||||
{
|
||||
public function posts() {
|
||||
$get = [
|
||||
'data' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'judul' => 'post_1',
|
||||
'status' => 1
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'judul' => 'post_2',
|
||||
'status' => 0
|
||||
],
|
||||
[
|
||||
'id' => 3,
|
||||
'judul' => 'post_3',
|
||||
'status' => 2
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$get['count'] = count($get['data']);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($get);
|
||||
}
|
||||
}
|
@ -3,36 +3,41 @@
|
||||
{% block title %}Mithril{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<main class="documentation">
|
||||
<section id="mit">
|
||||
</section>
|
||||
<main id="mit" class="documentation">
|
||||
</main>
|
||||
<script>
|
||||
var count = 0
|
||||
var root = document.getElementById('mit')
|
||||
var text = ''
|
||||
|
||||
var Hello = {
|
||||
var rend = {
|
||||
view: function() {
|
||||
return m("main", [
|
||||
m("h1", "Mithril"),
|
||||
m("code", "You do " + count + " clicks"),
|
||||
m("button", {onclick: function() {
|
||||
count++
|
||||
}}, "Click"),
|
||||
return m('section', [
|
||||
m('h1', 'Mithril'),
|
||||
m('p', text),
|
||||
m('button', {
|
||||
class: 'pseudo',
|
||||
onclick: getdata
|
||||
}, 'Click')
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
var Splash = {
|
||||
view: function() {
|
||||
return m("a", {href: "#!/hello"}, "Enter!")
|
||||
var getdata = function() {
|
||||
m.request({
|
||||
list: [],
|
||||
method: 'GET',
|
||||
url: '/api/posts',
|
||||
withCredentials: true
|
||||
})
|
||||
.then(function(get) {
|
||||
getdata.list = get.data
|
||||
})
|
||||
|
||||
for (var i = 0; i < getdata.list.length; i++) {
|
||||
text = text + getdata.list[i].judul + ' '
|
||||
}
|
||||
}
|
||||
|
||||
var root = document.getElementById('mit')
|
||||
|
||||
m.route(root, "/splash", {
|
||||
"/splash": Splash,
|
||||
"/hello": Hello,
|
||||
})
|
||||
m.mount(root, rend)
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user