Incorporating knockout into my project has been a great help as I retrieve JSON objects using Ajax. One question that arises is how to effectively utilize this data in my custom JavaScript code:
After receiving the mapped item from the ajax call, here is an example of what the object looks like:
MedarbejderId: ko.observable(element.MedarbejderId),
The MedarbejderId serves as the key to finding the corresponding name in the following method.
function TranslateMed(i)
{
console.log("test " + i) // for testing purposes in console.
for (var key in obj)
{
if (obj[key].Value == i)
{
console.log("vi er inde");
return obj[key].Text;
}
}
return obj[0].Text
}
The 'obj' variable contains a list of names and the translation works seamlessly. However, a challenge arises when trying to save the data,
$(document).on("click", ".kout-update", null, function (ev) {
var current = ko.dataFor(this);
console.log(current);
current.MedarbejderNavn = TranslateMed(current.MedarbejderId);
current.Mode("display");
saveData(current);
});
When dealing with current.MedarbejderId, it returns
c(){if(0<arguments.length)return c.Ua(c[F],arguments[0])&&(c.ia(),c[F]=arguments[0],c.ha()),this;a.l.sc(c);return c[F]}
. I actually need to retrieve its value instead.