I Versuch bin eine dynamische Datenbindungsfunktion assemble
genannt zu bauen, die (2) Eingangsparameter erfolgt:Binding dynamische Serverantwort (nested json)
server response (JSON)
- nested json Objekt.instruction set (JSON)
- ein Konfigurationsobjekt, das die Bindung steuert.
Das Problem: Die Funktion derzeit nicht verschachtelten json binden.
Die Frage: Was muss ich es in der Lage die gewünschte Ausgabe unterstützen zu machen ändern?
Der Stromausgang:
<form class="myForm">
<div class="myFirstName">john</div>
<div class="myLastName">doe</div>
<div>[email protected]</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>[email protected]</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>
Der gewünschte Ausgang:
<form class="myForm">
<div class="myFirstName">john</div>
<div class="myLastName">doe</div>
<div>[email protected]</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>[email protected]</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>
Die Serverantwort:
response=[
{
first: "john",
last: "doe",
email: "[email protected]",
profile:{
age: "26",
dob: "1990",
race: "white"
}
},
{
first: "jane",
last: "doe",
email: "[email protected]",
profile:{
age: "25",
dob: "1991",
race: "white"
}
}
]
Der Befehlssatz:
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
}
}
}
}
}
der Zusammenfügungsfunktion:
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;
}
Ok Ich habe das einfach kopiert und es wird kein "innerHTML" in irgendwas davon gesetzt. Die Art und Weise, wie ich es ursprünglich hatte, war "Inhalt: null" und sollte den "Wert" wie im aktuellen "prop: value" verwenden. Ich könnte auch, wenn ich einen String wie diesen Inhalt statisch hinzufügen wollte: 'my string'', und dann, wenn ich nichts wollte, würde ich die' content'-Requisite überhaupt nicht hinzufügen. –
Was willst du als html ?? Der Schlüssel ?? ... nur || k in der Zuweisung – fedeghe
Ja der aktuelle 'key: value'. Wenn ich console.log 'o [k]' gebe, gibt es genau das, was ich will, aber wenn sein Nest (Sub-Level) wie "Alter, Dob, Rasse" ist dann gibt es mir nur das Objekt. –