A little better Alpine.js usage by having GetJson just be an async function you use in x-init.

master
Zed A. Shaw 2 weeks ago
parent c5b62bf491
commit 70f9cb089b
  1. 18
      static/js/code.js
  2. 10
      views/admin/table/new.html
  3. 8
      views/admin/table/view.html
  4. 5
      views/post/view.html

@ -31,22 +31,10 @@ class PaginateTable {
} }
} }
class GetJson { const GetJson = async (url) => {
constructor(url) { const resp = await fetch(url);
console.assert(url !== undefined, "Invalid url is undefined");
console.log("GetJson url", url);
this.item;
this.url = url;
}
async item() {
const the_url = this.url;
const resp = await fetch(this.url);
console.assert(resp.status == 200, "failed to get it"); console.assert(resp.status == 200, "failed to get it");
return await resp.json();
this.item = await resp.json();
return this.item;
}
} }
const ConfirmDelete = async (table, obj_id) => { const ConfirmDelete = async (table, obj_id) => {

@ -1,13 +1,12 @@
<script>
let Data = new GetJson("/api/admin/new/table/{{ .Table }}");
</script>
<h1><a href="/admin/table/{{ .Table }}/">&laquo;</a>Admin {{ .Table }}</h1> <h1><a href="/admin/table/{{ .Table }}/">&laquo;</a>Admin {{ .Table }}</h1>
<block x-data="Data"> <block x-data="{item: {}}"
x-init="item = await GetJson('/api/admin/new/table/{{ .Table }}')">
<form method="POST" action="/api/admin/new/table/{{ .Table }}"> <form method="POST" action="/api/admin/new/table/{{ .Table }}">
<card> <card>
<top><h2>New {{ .Table }}</h2></top> <top><h2>New {{ .Table }}</h2></top>
<middle> <middle>
<template x-for="(value, key) in item"> <template x-for="(value, key) in item">
<div> <div>
@ -16,6 +15,7 @@
</div> </div>
</template> </template>
</middle> </middle>
<bottom> <bottom>
<button type="button"><a href="/admin/table/{{ .Table }}/">Back</a></button> <button type="button"><a href="/admin/table/{{ .Table }}/">Back</a></button>
<button class="hover:btn-alert" type="button">Clear</button> <button class="hover:btn-alert" type="button">Clear</button>

@ -1,10 +1,8 @@
<script>
let Data = new GetJson("/api/admin/table/{{ .Table }}/{{ .Id }}");
</script>
<h1><a href="/admin/table/{{ .Table }}/">&laquo;</a>Admin {{ .Table }}</h1> <h1><a href="/admin/table/{{ .Table }}/">&laquo;</a>Admin {{ .Table }}</h1>
<block x-data="Data"> <block x-data="{item: {}}"
x-init="item = await GetJson('/api/admin/table/{{ .Table }}/{{ .Id }}')">
<form method="POST" action="/api/admin/table/{{ .Table }}/{{ .Id }}"> <form method="POST" action="/api/admin/table/{{ .Table }}/{{ .Id }}">
<card> <card>
<top><h1>{{ .Table }} : {{ .Id }}</h1></top> <top><h1>{{ .Table }} : {{ .Id }}</h1></top>

@ -1,10 +1,7 @@
<script>
let theMessage = new GetJson("/api/message/1");
</script>
<block class="!gap-0 border-t-1 border-gray-600" <block class="!gap-0 border-t-1 border-gray-600"
x-data="{item: {}}" x-data="{item: {}}"
x-init="item = await theMessage.item()"> x-init="item = await GetJson('/api/message/1')">
<bar class="!p-0 mb-3"> <bar class="!p-0 mb-3">
<img src="/logo.png" style="width: 50px; height: 50px" class="aspect-square" /> <img src="/logo.png" style="width: 50px; height: 50px" class="aspect-square" />

Loading…
Cancel
Save