I'm attempting to build a dynamic data-binding function called assemble
which takes (2) input parameters:
server response (JSON)
- nested json object.instruction set (JSON)
- a configuration object which controls the binding.
The Problem: The function currently doesn't bind nested json.
The Question: What do I need to change to make it able to support the desired output?
The current output:
<form class="myForm">
<div class="myFirstName">john</div>
<div class="myLastName">doe</div>
<div>john.doe@gmail.com</div>
<div>this is a static inject element</div>
<div class="myNestedContainer">
//HERE IS MY PROBLEM
//RETURNS undefined
<span class="myUserAge">undefined</span>
<span class="myUserDob">undefined</span>
<span class="myRace">undefined</span>
</div>
</form>
<form class="myForm">
<div class="myFirstName">jane</div>
<div class="myLastName">doe</div>
<div>jane.doe@gmail.com</div>
<div>this is a static inject element</div>
<div class="myNestedContainer">
//HERE IS MY PROBLEM
//RETURNS undefined
<span class="myUserAge">undefined</span>
<span class="myUserDob">undefined</span>
<span class="myRace">undefined</span>
</div>
</form>
The desired output:
<form class="myForm">
<div class="myFirstName">john</div>
<div class="myLastName">doe</div>
<div>john.doe@gmail.com</div>
<div>this is a static inject element</div>
<div class="myNestedContainer">
<span class="myUserAge">26</span>
<span class="myUserDob">1990</span>
<span class="myRace">white</span>
</div>
</form>
<form class="myForm">
<div class="myFirstName">jane</div>
<div class="myLastName">doe</div>
<div>jane.doe@gmail.com</div>
<div>this is a static inject element</div>
<div class="myNestedContainer">
<span class="myUserAge">25</span>
<span class="myUserDob">1991</span>
<span class="myRace">white</span>
</div>
</form>
The server response:
response=[
{
first: "john",
last: "doe",
email: "john.doe@gmail.com",
profile:{
age: "26",
dob: "1990",
race: "white"
}
},
{
first: "jane",
last: "doe",
email: "jane.doe@gmail.com",
profile:{
age: "25",
dob: "1991",
race: "white"
}
}
]
The instruction set:
instruction={
tag:"form",
attributes:{"class":"myForm"},
children:{
first:{
tag:"div",
attributes:{"class":"myFirstName"},
content: null
},
last:{
tag:"div",
attributes:{"class":"myLastName"},
content: null
},
email:{
tag:"div",
content: null
},
myFakeTag:{
tag:"div",
content: "this is a static inject element"
},
profile:{
tag:"div",
attributes:{"class":"myNestedContainer"},
children:{
age:{
tag:"span",
attributes:{"class":"myUserAge"},
content: null
},
dob:{
tag:"span",
attributes:{"class":"myUserDob"},
content: null
},
race:{
tag:"span",
attributes:{"class":"myRace"},
content: null
}
}
}
}
}
The assemble function:
assemble=(data,instr)=>{
var instr = (typeof instr !== "string") ? instr : instr.split('.').reduce((o,i)=>o[i], model);
var n = new DocumentFragment();
var gen=(d)=>{
var o = d;
return(_=(_instr,k,_n)=>{
for(b in _instr){
switch(b){
case "tag":
var tag = document.createElement(_instr[b]);
break;
case "attributes":
for(a in _instr[b]){
tag.setAttribute(a,_instr[b][a]);
}
break;
case "events":
for(a in _instr[b]){
_instr[b][a].forEach((l)=>{
tag.addEventListener(a,l);
});
}
break;
case "content":
tag.innerHTML = (_instr[b]===null) ? o[k] : _instr[b];
break;
case "children":
for(var _i in _instr[b]){
_(_instr.children[_i],_i,tag);
}
break;
}
!!_n && !!tag && _n.appendChild(tag);
}
return tag;
})(instr, null);
};
(()=>{
for(i in data){
var test = gen(data[i]);
n.appendChild(test);
}
})();
return n;
}