如何创建动态流星js按钮,该按钮将与渲染的任何集合同步

How to create dynamic meteor js buttons that will sync with whatever Collection is rendered

本文关键字:按钮 同步 集合 任何 创建 动态 js 流星 何创建      更新时间:2023-09-26

我的应用程序有一个产品列表,这些产品可以根据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所指的包。