Right now, I'm using this JSON with the KO Mapping plugin and it's working fine:
{
"Controls": [
{
"Fields": [
{
"Name": "emailField",
"Text": "email",
"Visible": true
},
{
"Name": "hiddenField",
"Text": "text",
"Visible": true
}
],
"Name": "form2",
"Type": "Form"
},
{
"Data": [
[
"Federico Aloi",
20
],
[
"Andres Lopez",
31
],
[
"Pablo Perez",
32
]
],
"Fields": [
{
"Name": "nameField",
"Text": "Nombre",
"Visible": true
},
{
"Name": "ageField",
"Text": "Edad",
"Visible": true
}
],
"Name": "datagrid1",
"Type": "Datagrid"
}
],
"Name": "pagina1",
"Title": "Probando el KO"
}
Now my requirement is to perform "partial updates". Some scenarios when I'd like to do that:
- I need to change the Data array in the second control.
- I need to update only one Control and not the whole Page (this is the class I'm serializing, the root in this JSON).
- I need to add another Control to my Page.
Perhaps another workaround would be recreating the original object with ko.mapping.toJS(viewModel)
, change it and then re-map it again... but I believe that you will come out with something better.
EDIT: I tried with ko.mapping.fromJS(updatedControl, viewModel.Controls()[0])
but it didn't work, here is my code:
function (control) {
$.getJSON($.format('api/control/{0}/{1}', viewModel.Name(), control.Name()), function (response) {
ko.mapping.fromJS(response, viewModel.Controls()[0]);
});
},
response:
{
"Fields": [
{
"Name": "emailField",
"Text": "email",
"Visible": true
},
{
"Name": "hiddenField",
"Text": "text",
"Visible": true
}
],
"Name": "form2",
"Type": "Form"
}
EDIT2: check it out at http://jsfiddle.net/faloi/4FcAy/10/