Right now, I am using this JSON with the KO Mapping plugin and it works great:
{ "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 do "partial updates." Some scenarios when I would like to do this:
- I need to change the data array in the second control.
- I need to refresh 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 to recreate the original object using 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 that 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]); }); },
Answer:
{ "Fields": [ { "Name": "emailField", "Text": "email", "Visible": true }, { "Name": "hiddenField", "Text": "text", "Visible": true } ], "Name": "form2", "Type": "Form" }
EDIT2: check out http://jsfiddle.net/faloi/4FcAy/10/