The jQuery template does not actually implement two-way data binding, but another jQuery plugin developed by Microsoft does this. This Scott Guthrie post actually covers both the tmpl plugin and the Data Linking plugin. Scroll down to βClient Data Binding Support,β where Scott discusses in detail how the Data Linking plug-in works.
However, for two-way data binding, I find the knockoutjs extension much better and cleaner. declarative syntax keeps markup clean and custom data binding overrides allow many applications to be used. Finally, the plugin is pretty good for handling JSON from the server to the binding. Finally, knockoutjs also has bindings based on the tmpl plugin .
Good luck with your problem.
EDIT: Updated Code Example
Required Scripts:
<script src="/Scripts/jquery-1.5.0.min.js" type="text/javascript"></script> <script src="/Scripts/jquery.tmpl.js" type="text/javascript"></script> <script src="/Scripts/knockout.js" type="text/javascript"></script> <script src="/Scripts/knockout.mapping.js" type="text/javascript"></script>
Then here is the meat and potatoes
<div data-bind='template: { name: "tmplTest", foreach: viewModel.list }'> </div> <script type='text/html' id='tmplTest'> <div> <span data-bind='text: textvalue, uniqueName: true'></span> <input data-bind='value: textvalue, uniqueName: true, valueUpdate:"afterkeydown"'/> </div> </script> <script type='text/javascript'> var viewModel = ko.mapping.fromJS( { list:[ { textvalue: "text1" }, { textvalue: "text2"} ] }); $(function() { ko.applyBindings(viewModel); }); </script>
source share