Browser UI
->
Fetch Request
->
Backend API
->
JSON Response
->
DOM Update
Sie finden ein Pocketbase Backend unter der URL notes.iso.lmeier.ch
<form id="note-form">
<input id="title" placeholder="Titel" required />
<textarea id="content" placeholder="Notizinhalt" required></textarea>
<button type="submit">Speichern</button>
</form>
<ul id="notes-list"></ul>
const BASE_URL = "http://notes.iso.lmeier.ch";
const NOTES_ENDPOINT = `${BASE_URL}/api/collections/notes/records`;
const form = document.querySelector("#note-form");
const notesList = document.querySelector("#notes-list");
async function loadNotes() {
const response = await fetch(NOTES_ENDPOINT);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const notes = await response.json();
renderNotes(notes);
}
function renderNotes(notes) {
notesList.innerHTML = "";
for (const note of notes) {
const li = document.createElement("li");
li.textContent = `${note.title}: ${note.content}`;
notesList.append(li);
}
}
async function createNote(noteData) {
const response = await fetch(NOTES_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(noteData)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json();
}
form.addEventListener("submit", async (event) => {
event.preventDefault();
const noteData = {
title: document.querySelector("#title").value,
content: document.querySelector("#content").value
};
await createNote(noteData);
form.reset();
await loadNotes();
});
async function init() {
try {
await loadNotes();
} catch (error) {
console.error(error);
notesList.innerHTML = "<li>Fehler beim Laden der Notizen</li>";
}
}
init();