Javascript

Javascript

The Javascript part is first to send the "url" to Go. Go then creates the form and fill with data and sends back to the HTML body. Javacript fetches this rendered form and "paste" into a certain innerHTMl div.

The function that fetches the body and "paste" it into innerHTML

One function for each view, edit and new record. Writing to innerHTML means less flickering.

// send to Go endpoint and display in innerHTML
function get_rec(id) {
    let url = "/" + module + "/view/" + id
    fetch(url, {
    headers: {
        "Content-Type": "application/json"
    },
    credentials: "same-origin"
    })
    .then((res) => res.text())
    .then((response) => {
        document.getElementById("subcnt").innerHTML = response; /* writes to innerHTML */
    })
    .catch((error) => alert("Error get_rec:", error));
    }

//new record
function new_rec() {
    let url = "/" + module + "/new/"
    fetch(url, {
    headers: {
        "Content-Type": "application/json"
    },
        credentials: "same-origin"
    })
    .then((res) => res.text())
    .then((response) => {
        document.getElementById("subcnt").innerHTML = response;
    })
    .catch((error) => alert("Error new_rec:", error));
}

// send to Go endpoint and display in innerHTML
function edit_rec(id) {
    let url = "/" + module + "/edit/" + id
    fetch(url, {
    headers: {
        "Content-Type": "application/json"
    },
    credentials: "same-origin"
    })
    .then((res) => res.text())
    .then((response) => {
        document.getElementById("subcnt").innerHTML = response;
    })
    .catch((error) => alert("Error get_rec:", error));
}