javascript - jQuery templates plugin: how to create two-way binding? -


i started using jquery templates plugin (the 1 microsoft created), face problem: template bunch of forms bound array of objects; when change on 1 of forms, want bound object update , can't figure out how automate that.

here's simple example (real life template , object more complex) :

<!-- template --> <script type="text/html" id="tmpltest">     <input type="text" value="${textvalue}"/> </script>  <!-- object bind --> <script type="text/javascript">     var obj = [{textvalue : "text1"},{textvalue : "text2"}]      jquery("#tmpltest").tmpl(obj) </script> 

this populate 2 textboxes, each bound value corresponding object. now, if change value in 1 of textboxes, need update corresponding data object's value. idea how that?

jquery template doesn't implement two-way data binding, microsoft developed jquery plugin does. scott guthrie post covers both tmpl plug in , data linking plugin. scroll down "support client data-linking" scott goes detail on how data linking plug in works.

however, 2 way data binding, find knockoutjs extension better , cleaner. declarative syntax keeps markup clean , custom data binding overrides allow multitude of applications. mapping plugin pretty great processing json server binding. knockoutjs has bindings based on tmpl plugin well.

good luck problem.

edit: updated code example

scrips required:

<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 meat , potatoes

<!-- div loading template --> <div data-bind='template: { name: "tmpltest", foreach: viewmodel.list }'>     </div>  <!-- template --> <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> 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -