怎么算总评论?

时间:2016-02-10 19:01:20

标签: javascript mongodb meteor

我想计算特定食谱的总评论,并希望在 recipes.html 页面上显示其评论,其中显示了所有食谱。 当我点击一个食谱然后我可以通过 countReviews 帮助来计算它的评论,但是这个帮助器在recipes.html页面上不起作用,即使它是一个全球帮助器。所以你能帮我算一下评论吗食谱并在 recipes.html 上显示它们。

完整的源代码Github

collections.js

Recipes = new Mongo.Collection('recipes');
Reviews = new Mongo.Collection('reviews');
RecipesImages = new FS.Collection("recipesImages", {
    stores: [new FS.Store.GridFS("recipesImages")]
});

reviews.js

 Template.reviews.helpers({
        'showReviews': function () {
            return Reviews.find({recipeId: Router.current().data()._id})
        },

        countReviews: function(){
            return Reviews.find({recipeId: Router.current().data()._id}).count();
        }});

add_reviews.js

Template.add_review.events({
    'submit .add-review':function(event){
        event.preventDefault();
        var rating = event.target.rating.value;
        var review = event.target.review.value;
        var recipeId = Router.current().data()._id;
        addReview(rating,review,recipeId);
    }
});

recipes.html

<template name="recipes">
    <div class="container">
        <div class="row">
            {{#each showRecipes}}
                {{#if showRecipes}}
                    <div class=" col-md-4">
                        <a class=".deco-none" href="{{pathFor 'show_recipe'}}">
                            <div class="panel panel-default mb " >
                                <div class="panel-image">
                                    <img src="{{images.url storage='recipesImages'}}" class="panel-image-preview" />
                                </div>
                                <div class="panel-body pb">
                                    <h4>{{name}}</h4>
                                    {{shortText description 100}}
                                </div>
                                <div class="  panel-footer text-center" >
                                    <a href="{{pathFor 'show_recipe'}}" data-toggle="tooltip" title="View Recipe"><span class="glyphicon glyphicon-open-file"></span></a>
                                    <a href="#" data-toggle="tooltip" title="Cooking Time"><span class="glyphicon glyphicon-time" style="vertical-align:middle"></span><small> {{time}} Minutes</small></a>
                                    <a href="#" data-toggle="tooltip" title="Like it" data-action="addLikes"><span class=" glyphicon glyphicon-heart" style="vertical-align:middle"></span>&nbsp; <small>{{likes}} Likes </small></a>
                                    <a href="{{pathFor 'reviews'}}" data-toggle="tooltip" title="Reviews"><span class="glyphicon glyphicon-comment"></span></a>
                                </div>
                            </div>
                        </a>

                    </div>
                    {{/if}}
            {{else}}
                <h3>There are no recipes to show.Be the first one to add a recipe.<span class="required">(Log in required)</span></h3>

            {{/each}}
        </div>


    </div>

    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>
</template>

methods.js

addReview = function(rating,review,recipeId){
    if(review!=""){
        Reviews.insert({
            rating:rating,
            review:review,
            recipeId:recipeId
        });
        Router.go('reviews',{_id:recipeId});

        FlashMessages.sendSuccess('Review Added',{ autoHide: true, hideDelay: 2000 });
    }
    else{
        FlashMessages.sendError('Review field is empty',{ autoHide: true, hideDelay: 3000 });
    }
    return false;
};

upvote = function(currentRecipe){

    var user = Meteor.user();
    if(!user){
        FlashMessages.sendError("You need to login to like this recipe", {hideDelay: 1000});
        return false;

    }
    if (currentRecipe) {
        if (_.contains(currentRecipe.voters, Meteor.userId())) {
            FlashMessages.sendError("You already liked this recipe", {hideDelay: 1000});
            return false;
        }
        Recipes.update(currentRecipe._id, {$addToSet: {voters: 
Meteor.userId()}, $inc: {likes: 1}});
    }
};

1 个答案:

答案 0 :(得分:1)

试试这个让我知道:

<强>辅助

Template.recipes.helpers({
  countReviews: function() {
    return Reviews.find({recipeId:this._id}).count();
  }
});

<强> HTML

<a href="{{pathFor 'reviews'}}" data-toggle="tooltip" title="Reviews">
  <span class="glyphicon glyphicon-comment"></span>
  <small>{{countReviews}} reviews</small>
</a>