Grouping members of a collection by category in a Meteor template

故事扮演 提交于 2019-12-04 18:45:20

I don't think your implementation leaves that much to improve though maybe the below is a bit clearer by avoiding this.

Sorry I don't know Coffeescript so figured I should avoid confusion by writing javascript.

Template.categories.getCategories = function(){  
  var categories = Workouts.find({}, {sort: {category: 1}, fields: {category: 1}}).fetch();

  return _.uniq( categories, true, function (workout){ 
    return workout.category;
  });    
};                                            

Template.categories.workouts = function ( category ){
  return Workouts.find( { category: category}, { sort: {name : 1}});
};

<template name="categories">                                                       
  {{#each getCategories}}                                                          
    <h2>{{category}}</h2>                                                              
    {{#each workouts category}}
      {{> workout}}                                                                 
    {{/each}}
  {{/each}}                                                                        
</template>                                                                        

<template name="workout">                                                                                                               
  <div class="workout container">                                                
    <div>                                                                        
      {{name}}                                                                   
    </div>                                                                       
    <div>                                                                        
      {{units}}                                                                  
    </div>                                                                       
  </div>                                                                                                                                            
</template>  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!