Knockout JS mapping - Partial update
http://stackoverflow.com/questions/10213104/performing-partial-updates-with-knockoutjs-mapping-plugin/10218885
HTML
<script src="https://github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.debug.js"></script>
<script src="https://raw.github.com/SteveSanderson/knockout.mapping/master/knockout.mapping.js"></script>
<script id="Form" type="text/html">
<section data-bind="attr: { id: Name }">
<button data-bind="click: $root.getControl" style="float: right">
<img src="http://www.coolmanoide.com/wp-content/plugins/tweet-blender/img/ajax-refresh-icon.gif" alt="Refresh" />
</button>
<h2 data-bind="text: Name"></h2>
<form data-bind="foreach: Fields">
<input data-bind="value: Text, name: Name, visible: Visible" ></input>
</form>
</section>
</script>
<script id="Datagrid" type="text/html">
<section data-bind="attr: { id: Name }">
<button data-bind="click: $root.getControl" style="float: right">
<img src="http://www.coolmanoide.com/wp-content/plugins/tweet-blender/img/ajax-refresh-icon.gif" alt="Refresh" />
</button>
<h2 data-bind="text: Name"></h2>
<table>
<thead>
<tr data-bind="foreach: Fields">
<th data-bind="text: Text"></th>
</tr>
</thead>
<tbody data-bind="foreach: Data">
<tr data-bind="foreach: $data">
<td data-bind="text: $data"></td>
</tr>
</tbody>
</table>
</section>
</script>
<article data-bind="attr: { 'data-name': Name }" >
<h1 data-bind="text: Title"></h1>
<!-- ko template: { name: controlTemplate, foreach: Controls } -->
<!-- /ko -->
</article>
CSS
section
{
background: white;
border: solid black 1px;
border-radius: 2px;
margin: 10px 0;
padding: 8px;
}
section h2 {
font-variant: small-caps;
margin: 0;
}
table
{
margin: 5px 0;
border: solid gray 1px;
padding: 2px;
}
table th
{
font-weight: bold;
padding: 0 8px;
}
table td
{
padding: 0 8px;
}
JavaScript
var viewModel = null;
var pageModelFunctions = {
getControl: function(control) {
var fakeResponse = {
"Fields": [
{
"Name": "emailField",
"Text": "email",
"Visible": true},
{
"Name": "hiddenField",
"Text": "text",
"Visible": true}
],
"Name": "form3",
"Type": "Form"
};
/*$.getJSON($.format('api/control/{0}/{1}', viewModel.Name(), control.Name()), function(response) {*/
ko.mapping.fromJS(fakeResponse, {}, viewModel.Controls()[0]);
/*});*/
},
controlTemplate: function(control) {
return control.Type();
}
};
RefreshPage = function(pageId) {
$.getJSON('api/pages/' + pageId, function(data) {
ko.mapping.fromJS(data, viewModel);
});
};
// $.getJSON($.format('api/pages/{0}', 'pagina1'), function (data) {
var data = {
"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"
};
viewModel = $.extend(ko.mapping.fromJS(data),...