Loading data using Iron Router and Meteor

吃可爱长大的小学妹 提交于 2019-12-13 05:59:29

问题


Here is my dilemma. I'm currently loading my categories using a static variable onto the 'category' page. Everything loads and the links are clickable but when a user clicks on the category. The 'categoryPage' won't load the respective images that belong to that category. Here is my code.

categoryPage HTML:

<template name="categoryPage">
    <div class="text-center light-container" id="home-section">
        <h1>{{categoryName}}</h1>
        <hr/>
        <div class="row">
            {{#each categoryGifs}}
                <a href="{{pathFor 'gif'}}">
                    <img class="freezeframe gifs" data-id="{{_id}}" src="{{image}}"/>
                </a>
            {{/each}}
        </div>
    </div>
</template>

categoryPage JS:

Template.categoryPage.helpers({
    // Load 16 most recent ones
    // When down arrow is click load another 16 more gifs
    'categoryName': function(){
        var category = this.params.category;
        return category;
    },
    'categoryGifs': function() {
        var category = this.params.category;
        console.log(category);
        return GifsCollection.find({category: category}, {fields: {_id: 1, image: 1, category: 1} });
    }
});

Router JS:

Router.route('/categories', {
    name: 'categories',
    template: 'categories',
    fastRender: true
});
Router.route('/categories/:category', {
    name: 'categoryPage',
    template: 'categoryPage',
    data: function(){
        var category = this.params.category;
        return GifsCollection.find({category: category});
    },
    fastRender: true
});

回答1:


in 'categoryGifs': function(), change

    var category = this.params.category;

with:

    var category = Router.current().params.category;

here is the working code:

http://meteorpad.com/pad/AdRS8mfyHsZjA2Rvp/Leaderboard



来源:https://stackoverflow.com/questions/32911969/loading-data-using-iron-router-and-meteor

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