我的应用程序有一个产品列表,这些产品可以根据id(由其产品类型或类别决定,请参阅下文)通过按钮进行排序。
<template name="filterbuttons">
<div class="filter-button-container">
<button id="foo" type="submit" name="foo">Foo</button>
<button id="bar" type="submit" name="bar">Bar</button>
有时用户会导航到不同类别的产品,我需要更新按钮来反映这一点。
<template name="filterbuttons">
<div class="filter-button-container">
{{#each product }}
<button id="{{ product.type }}" type="submit" name="{{ product.type }}">{{ product.type }}</button>
现在是陷阱。我的列表中包含重复项,所以我还必须循环遍历并只挑选唯一的product.type
值。有没有一种方便的方法可以用模板(流星闪耀)做到这一点?或者这需要一个助手,比如。。。
Template.filterbuttons.helpers({
product: function () {
// ...
// use _.uniq() to remove duplicates
return {type: {['foo', 'bar']}}
}
});
我还没有尝试过,但根据这一点,Meteor 0.9允许您在模板中使用下划线。
现在,您可以直接在Blaze模板中使用下划线。这是Gwendall Esnault的一个非常简单的包装的结果。这是一个方便的实用程序。
Undercore Helper是MeteorHacks所指的包。