Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Created May 1, 2017 12:13
Show Gist options
  • Save bhameyie/909e2c6c3977b6486ed5552eb19bbaa3 to your computer and use it in GitHub Desktop.
Save bhameyie/909e2c6c3977b6486ed5552eb19bbaa3 to your computer and use it in GitHub Desktop.
Rendering HAL Extension
function buildForm(containerId,obj) {
if (!obj["_presentable"]) {
return;
}
var presentable = obj["_presentable"];
var links = obj["_links"];
var form = document.createElement("form");
var formLink = links[presentable.actionBoundTo][0].href
form.setAttribute("method", "POST");
form.setAttribute('action', formLink);
presentable.fields.forEach(function(element) {
var elemContainer = document.createElement("div");
elemContainer.setAttribute("class", "form-group");
var label = document.createElement("label");
label.innerText=element.name;
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", element.boundTo);
if (!element.editable){
input.setAttribute("readonly","true");
}
if(element.restrictions.indexOf('Required') > -1){
input.setAttribute("required","true");
}
var bindings = element.boundTo.split('.');
var inputVal = obj[bindings[0]];
if (inputVal){
for (i = 1; i < bindings.length; i++) {
inputVal = inputVal[bindings[i]];
}
input.setAttribute("value",inputVal);
}
elemContainer.appendChild(label);
elemContainer.appendChild(input);
form.appendChild(elemContainer);
});
var submit = document.createElement("button");
submit.setAttribute("class", "btn btn-success");
submit.setAttribute("type", "submit");
submit.innerText = links[presentable.actionBoundTo][0].name;
form.appendChild(submit);
var container = document.getElementById(containerId);
container.innerHTML = '';
container.appendChild(form);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment