1
想知道在下劃線模板中使用閉包是否有任何價值...說跟蹤計數器或其他東西。下面是我的意思一個簡單的例子:在underscorejs模板中使用閉包
<%
(function(models){
var length = models.length-1,
section = "";
_.each(models, function (item, index) {
if (index === 0) {
section = "top";
} else if (index === length) {
section = "bottom";
} else {
section = "center";
}
%>
<div class="container">
<div class="gradiantDiv <%= section %>content">
<a href="/#customer/<%= item._id %>">
<address>
<strong><%= item.name %></strong><br>
<%= item.addr1 %><br>
<%= item.city %>, <%= item.state %> <%= item.zip %><br>
<abbr title="Phone">P:</abbr> <%= item.phone %>
</address>
</a>
</div>
<div class="gradiantDiv <%= section %>action">
<i class="icon-chevron-right"></i>
</div>
</div>
<%
});
})(models);
%>
還是隻是更好的聲明,如「長」和「節」變量沒有_.each前關閉?或者它有什麼關係?
謝謝!