Possible jsviews bug with {^{if}}

情到浓时终转凉″ 提交于 2019-12-13 07:23:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!