var CreateViewModel = function (item,array) {
var self = this;
//default the page to 1 for first call
self.thisData = ko.observable(item);
self.arrayData=ko.observableArray(array);
}
$("#click1").click(function () {
ko.applyBindings(CreateViewModel("one",array1),$("#one")[0]);
});
$("#click2").click(function () {
ko.cleanNode($("#two")[0]);
ko.applyBindings(CreateViewModel("two",array2),$("#two")[0]);
});
$("#click3").click(function () {
ko.applyBindings(CreateViewModel("two",array2));
});
<button id="click1">Set Data 1</button>
<div id="one">
<span>Result1 : </span> <span data-bind="text: thisData"></span>
<br/>
<span>Result1 sub item : </span> <span data-bind="text: arrayData()[2].userName"></span>
<br/>
<table>
<tbody id="body1" data-bind="foreach: arrayData">
<td >
<span data-bind='text: userName'/>
</td>
<td >
<span data-bind='text: userAge'/>
</td>
</tbody>
</table>
</div>
<br/>
<button id="click2">Set Data 2</button>
<br/>
<div id="two">
<span>Result2 : </span> <span data-bind="text: thisData"></span>
<br/>
<span>Result2 sub item : </span> <span data-bind="text: arrayData()[2].userName"></span>
<table>
<tbody data-bind="foreach: arrayData">
<td >
<span data-bind='text: userName'/>
</td>
<td >
<span data-bind='text: userAge'/>
</td>
</tbody>
</table>
</div>
<br/>
<button id="click3">Set Global</button>
You should never bind an element more than once. Manipulate the model, based on user input, but never rebind.
--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi ,