From b38b14e2f64340454bbb7a62fb05a06830de0de6 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 23 Feb 2024 20:55:27 -0500 Subject: [PATCH] Accept is way too complicated, so I'll have to investigate other ways this is done. --- controllers/todo_alpine.js | 21 +++++++-------------- templates/todo_alpine.html | 29 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/controllers/todo_alpine.js b/controllers/todo_alpine.js index 80355f0..dea9fc4 100644 --- a/controllers/todo_alpine.js +++ b/controllers/todo_alpine.js @@ -1,21 +1,15 @@ import { ToDo } from "../lib/models.js"; import nunjucks from "nunjucks"; -// this can go away when I learn how to detect the request type -export class TodoIndex { - async get(req, rep) { - const todo_list = await ToDo.all({}); - - const result = nunjucks.render("todo_alpine.html", {name: "Zed"}); - - rep.code(200).type("text/html").send(result); - } -} - export class Todo { async get(req, rep) { - const todo_list = await ToDo.all({}); - rep.code(200).send(todo_list); + if(req.headers.accept === "application/json") { + const todo_list = await ToDo.all({}); + rep.code(200).send(todo_list); + } else { + const result = nunjucks.render("todo_alpine.html", {name: "Zed"}); + rep.code(200).type("text/html").send(result); + } } async post(req, rep) { @@ -33,5 +27,4 @@ export class Todo { // I mean, it works not sure if I like it though export default { todo_alpine: Todo, - todo_index: TodoIndex }; diff --git a/templates/todo_alpine.html b/templates/todo_alpine.html index ba0241e..4362306 100644 --- a/templates/todo_alpine.html +++ b/templates/todo_alpine.html @@ -5,8 +5,29 @@ @@ -14,7 +35,7 @@
+ x-init="todos = await list_todo()">