0

我想为传入编译的所有对象添加 _first = true 到第一个对象。_first = true 应该添加到 data 和 company 仅用于第一个数组。

模板

<script id="firsttemp" type="text/x-handlebars-template">

        {{#each objects}}
        <tr>

        <td class="tableFirstCol"><input type="checkbox" class="check" id="childcheck" /></td>
        <td class="tableFirstCol"><input type="checkbox" class="star" id="childstar" /></td>
        <td class="tableFirstCol"><input type="checkbox" class="alarm" id="childalarm" /></td>
        <td class="tableFirstCol">{{name}}<span>{{name}}</span></td>

        </tr>
        {{/each}}
</script>

json

company =    {
            "name": "IBM",
            "lastprice": 310.2,
            "dayschange": 120.1,
            "dayshigh": 220.9,
            "volume": 140.3,
            "52weekhigh": 200.3,
            "name2": "herohonda",
            "dataset": [1200,2000,5200],
            "lastprice2": "1:10pm",
            "dayschange2": "8%",
            "dayshigh2": "1.5%",
            "volume2": "1:10 Pm",
            "52weekhigh2": "1:10 Pm"
        },
         {
            "name": "zbHero Honda",
            "lastprice": 310.2,
            "dayschange": 220.1,
            "dayshigh": 220.9,
            "volume": 140.3,
            "52weekhigh": 200.3,
            "name2": "herohonda",
            "dataset": [3200,3500,5000],
             "dataset1":[20,+54,1000],
            "lastprice2": "1:10pm",
            "dayschange2": "8%",
            "dayshigh2": "1.5%",
            "volume2": "1:10 Pm",
            "52weekhigh2": "1:10 Pm"
        }

编译:

$("#row-2-td-1-table").html( template({objects:company}) );  
$("#row-2-td-1-table").html( template({objects:data}) );  

结果应该是:

company =    {
            "_first" : true,
            "name": "IBM",
            "lastprice": 310.2,
            "dayschange": 120.1,
            "dayshigh": 220.9,
            "volume": 140.3,
            "52weekhigh": 200.3,
            "name2": "herohonda",
            "dataset": [1200,2000,5200],
            "lastprice2": "1:10pm",
            "dayschange2": "8%",
            "dayshigh2": "1.5%",
            "volume2": "1:10 Pm",
            "52weekhigh2": "1:10 Pm"
        },
         {
            "name": "zbHero Honda",
            "lastprice": 310.2,
            "dayschange": 220.1,
            "dayshigh": 220.9,
            "volume": 140.3,
            "52weekhigh": 200.3,
            "name2": "herohonda",
            "dataset": [3200,3500,5000],
             "dataset1":[20,+54,1000],
            "lastprice2": "1:10pm",
            "dayschange2": "8%",
            "dayshigh2": "1.5%",
            "volume2": "1:10 Pm",
            "52weekhigh2": "1:10 Pm"
        }
4

1 回答 1

0

模板:

<tr  {{#equal @index}}{{/equal}}>
</tr>

Regitry 助手:

Handlebars.registerHelper('equal', function(lvalue, options) {

    if( lvalue==0 ) {
        return "id=firstTr-1";
    } 
});
于 2013-03-14T12:49:05.137 回答