Background:
I was working on a personal project to create an Task Checklist Web App. I worked on the code with vanilla golang and javascript. It is a very simple app with limited capability. I've used a json-rpc like endpoints for the application.
Code and Implementations:
Endpoints:
/getTsk GET
/addTsk POST
/delTsk POST
Html implementation of the ajax updated div:
<!-- Pending task loads here -->
<!-- On reload, everything in this div is deleted -->
<!-- The tasks div are then added into this div -->
<div id="pendingTasks"></div>
Ajax Javascript Code to add new task:
function saveTasks(value) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == request.DONE) {
if (request.status === 200) {
reloadPending(); //If sucessfully added task, then reload the corresponding div
showAlert("New Task Added.", "alert-success");
} else {
showAlert("Failed to Add New Task, Please try again later.", "alert-danger");
}
}
};
request.open("POST", ep_prefix + "/addTsk");
request.setRequestHeader("Content-type", "application/json");
request.send(JSON.stringify({ description: value }));
}
Ajax Javascript to reload the div after sucessful addition of task:
function reloadPending() {
let content = document.getElementById("pendingTasks");
content.innerHTML = "";
makeGetRequest("GET", "/getTsks", initPending, showEmptyPending, "?status=0"); //Get all the task from backend again
}
Population of task elements in the div:
//Population of the entries
let fragment = document.createDocumentFragment();
console.log(tasks.length);
for (let i = 0; i < tasks.length; i++) {
console.log(tasks[i]);
let taskElements = createTask(tasks[i], "pending");
fragment.appendChild(taskElements);
}
document.getElementById("pendingTasks").appendChild(fragment);
Problem:
I've implemented my app in such a way that, when a post request is sent to the endpoints, it will only return a success code(http 200) and I will have to make another ajax get request to get the data from the backend and update the DOM element, which causes flickering and is not smooth and also not practical, considering if I have 100 elements i will need to get all those elements again just to add one element.
Question:
How should I design the endpoints or my web application such that the addition of a new task will not require a full reload(Get all task again from backend) and update of the div?
Thank you.
/getTsk
instead of/getTask
? You don't like vowels? ;-)GET /task
,POST /task
, andDELETE /task
instead of usingPOST
and putting the action in the endpoint (like/delTask
for, I assume, deleting tasks)?