问题
In the example below, if I add several guests and then remove the 1st guest the entire guest table disappears and never reappears even though the Guest[] in not empty. Is there something wrong with {^{if}} here or my code? (I'm using commit 34)
<div id="frm-reg"></div>
<script id='jsr-registration' type='text/x-jsrender'>
<p><button id="btn-addGuest">Add Guest</button></p>
{^{if Guests.length}}
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th></th>
</tr>
{^{for Guests}}
<tr>
<td><input data-link="FirstName" type="text" /></td>
<td><input data-link="LastName" type="text" /></td>
<td><input data-link="Age" type="text" /></td>
<td><button class="btn-remove">Remove</button></td>
</tr>
{{/for}}
</table>
{{/if}}
</script>
<script>
var model={"CampID":3,"FirstName":"a","LastName":"b","Guests":[]};
$.templates({regTmpl: "#jsr-registration"});
$.link.regTmpl("#frm-reg", model)
.on("click", ".btn-remove", function()
{
$.observable(model.Guests).remove( $.view(this).index, 1 );
return false;
});
$("#btn-addGuest").click(function()
{
$.observable(model.Guests).insert(model.Guests.length, {FirstName:"", LastName:"", Age:""});
return false;
});
</script>
回答1:
This is the result of not having a <tbody>
tag in the markup.
When you use <table>
tags in JsRender templates, used with JsViews data-binding, you need to remember always to include a <tbody>
tag. The browser will insert one anyway, and JsViews parsing will then end up with incorrect element depth information.
I will add a warning error message in the JsViews parser for this.
来源:https://stackoverflow.com/questions/15764115/possible-jsviews-bug-with-if