Underscore模板在循环内未定义

时间:2014-01-19 02:47:58

标签: javascript underscore.js

使用下划线模板。循环通过骨干集合:

  <% _.each(venues, function (venue) { %>
  <tr>
    <td class="text-muted"><%= venue.get('city') %></td>
    <td class="text-muted"><%= venue.get('name') %></td>
    <td class="text-muted"><%= venue.get('live') == true ? "Yes" : "No" %></td>
    <td class="text-muted">$0.00</td>
    <td class="blank controls">
      <a href="#"><span class="icon icon-edit"></span><span class="text-hide">Edit</span></a>
    </td>
  </tr>
  <% }); %>

我明白了:

Uncaught TypeError: Cannot call method 'get' of undefined 

1 个答案:

答案 0 :(得分:1)

如果场地是Backbone.Collection,那么它将混合所有下划线迭代方法。

尝试:

<% venues.each(function (venue) { %>
  <tr>
    <td class="text-muted"><%= venue.get('city') %></td>
    <td class="text-muted"><%= venue.get('name') %></td>
    <td class="text-muted"><%= venue.get('live') == true ? "Yes" : "No" %></td>
    <td class="text-muted">$0.00</td>
    <td class="blank controls">
      <a href="#"><span class="icon icon-edit"></span><span class="text-hide">Edit</span></a>
    </td>
  </tr>
<% }); %>