1

由于某种原因,尝试使用 underscore.js 将名称从该 json 中提取出来,但在通过结果时遇到了麻烦。我在 underscore.js 模板方面取得了一些成功,但不是像这样的 json。

<script type="text/html" id='usageList'>
    <table cellspacing='0' cellpadding='0' border='1'>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>

            <% _.each(deals,function(item,key,list){ %>
                <% var f = item.Name; %><!-- create more variables -->
                <tr>
                    <!-- use variables -->
                    <td><%= key %></td>
                    <td class="<%= f %>"><%= item.name %> </td>
                </tr>
            <% }); %>
        </tbody>
    </table>
</script>

<div id="target"></div>

<script>

    var deals = { 
        "results" : [ 
            {
                "geometry" : {
                    "location" : {
                        "lat" : 35.2521950,
                        "lng" : -120.6917010
                    }
                },
                "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
                "id" : "a60236447e99ae2b2fd0cde9b912f7abd91f4e1d",
                "name" : "Costco",
                "opening_hours" : {
                    "open_now" : true
                },
                "price_level" : 2,
                "reference" : "CqQBlgAAAJukesLD8HeY7_xb-   V51usJ1nVXJjiWVT3DRYVbParEaKTO8881ULz4f2RM8nGKVOHdIdM67oujeVWFA0YW63_ySzH8O9zTPPwT7h8MijD0BsKnue1BUb_eYHvIp4bCREitCI-b7LIJP_a4UzCrxzT8UsW28KYIParQKJJNfnDMXVvM2ymsvWiRGA42qre6Rhkn7DWkuGyG3zvgZ3xKLz1wSEGl0gCL7B5GNwBSRBytKgmwaFIh1TU7nJCMMsBj04tlA87HHh3Y3",
                "types" : [
                    "department_store",
                    "health",
                    "gas_station",
                    "grocery_or_supermarket",
                    "food",
                    "store",
                    "establishment"
                ],
            "vicinity" : "1540 Froom Ranch Way, San Luis Obispo"
            }
        ]
    };

    var template = $("#usageList").html();
    $("#target").html(_.template(template,{deals:results}));

</script>
4

1 回答 1

1

认为您的模板调用不正确:

$("#target").html(_.template(template,{deals: deals.results}));

“结果”值是“交易”全局引用的对象的属性,因此您需要以这种方式引用它。

于 2012-12-28T17:20:56.717 回答