Grouping members of a collection by category in a Meteor template

偶尔善良 提交于 2019-12-12 09:15:33

问题


I have a single Workouts collection, each object having a category and a name. At the HTML layer, I'm creating a category header, then displaying a list of workouts beneath.

I have it working, but there's no way I'm doing it the meteor and/or handlebars way. Think I'm struggling with understanding the context of this, as well as how to pass data into a template.

Here are my template functions:

Template.categories.getCategories = ->                                             
  workouts = Workouts.find().fetch()                                               
  categories = _.map workouts, (workout) -> workout.category                       
  _.uniq categories                                                                

Template.workouts.getWorkouts = ->                                                 
  Workouts.find {category: this.toString()},                                       
    sort: ['category', 'name']

And here's the template file:

<head>                                                                                                                         
</head>                                                                            

<body>                                                                                                                              
  {{> categories}}                                                                 
</body>                                                                            

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

<template name="workouts">                                                         
  {{#each getWorkouts}}                                                            
    <div class="workout container">                                                
      <div>                                                                        
        {{name}}                                                                   
      </div>                                                                       
      <div>                                                                        
        {{units}}                                                                  
      </div>                                                                       
    </div>                                                                         
  {{/each}}                                                                        
</template>  

Inside Template.categories.getCategories, this is Window. But inside of Template.categories.getCategories, it's the string object equivalent of the this inside the categories template, which is a string literal. Huh?

Again, this works. I get a category heading with a list of workouts beneath, but what's a better way of doing this?


回答1:


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>  


来源:https://stackoverflow.com/questions/19896769/grouping-members-of-a-collection-by-category-in-a-meteor-template

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