Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<script type="text/html" id="person-template">
 <tr><td>
       <p>Book Title: <h3 data-bind="text: Title"></h3>       
       <p>Available City: <span data-bind="text: CityName"></span></p>
</td></tr>
</script>


<table id="sample">					
<tbody id="list" data-bind="template: { name: 'person-template', foreach: people }" >
</tbody>                                
</table>


  jQuery(document).ready(function () { 
      
        var loData = AjaxCall('default.aspx', 'GetBookData', '10');
        var Data = JSON.parse(loData.Data);

        function MyViewModel() {
            this.people = Data.Table
        }
        ko.applyBindings(new MyViewModel()); 
       
    });
	
	
	$("#btnChangData").click(
	
        var loData = AjaxCall('default.aspx', 'GetBookData', '20');
        var Data = JSON.parse(loData.Data);           
        
        ko.cleanNode($("#list")[0]);
        $('#sample').empty();
        $('#sample').html('<tbody id="list" data-bind="template: { name: "person-template", foreach: people }" ></tbody>');
        function MyViewModel() {
            this.people = Data.Table
        }
        ko.applyBindings(new MyViewModel());
		
		);




I have load data on document ready function to table. but in button click event i need to load new datasource to table, how can rebind datasource to knockout template.. in click event above code throws
C#
You cannot apply bindings multiple times to the same element
error on console..

What I have tried:

i will explain everything in question
Posted
Updated 26-Feb-16 15:54pm

1 solution

I have tried below code and its worked for me, i have declare viewmodel globally var vm; var viewModel = function () { var self = this; this.people = ko.observableArray([]); this.updatedata = function (data) { ko.mapping.fromJS(data, {}, self.people); } }; and on page load vm = new viewModel(); vm.updatedata(Data.Table); ko.applyBindings(vm); and for data update vm.updatedata(Data)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900