An even more educational version of the Bandolier for Learn JS the Hard Way. https://learnjsthehardway.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
bandolier2/controllers/todo_alpine.js

38 lines
947 B

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);
}
async post(req, rep) {
if(req.query.todo_id !== undefined) {
await ToDo.delete({id: req.query.todo_id});
} else {
await ToDo.insert({task: req.body.task});
}
// this is how you can prevent the annoying double submit
rep.code(200).send({message: "OK"});
}
}
// you use the export default to set the route URL?
// I mean, it works not sure if I like it though
export default {
todo_alpine: Todo,
todo_index: TodoIndex
};