简体   繁体   中英

Loading data using Iron Router and Meteor

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
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM