Вопрос

I have a simple list and details view using two collections. When I navigate back to the list view Meteor removes the single document added to the details collection and undoes the change to the other collection.

I want this data to remain so the client doesn't have to keep reloading it...

Both the 'league' and the 'standings' subscriptions are 'undone' on navigation back to the the root. The league and leagues route both use the 'weeks' Mongo collection. When navigating to a league detail I add to the single document. Navigation to the detail works fine ... its when I navigate back to the list that I loose the collection data.

I need all this data 'cached' and am obviously not going about it correctly....

Router.map(function () {
    this.route('leagueList', {
        path: '/'
    });

    this.route('league', {
        path: '/league/:league',
        template: 'standings',
        waitOn: function () {
            console.log(this.params.league);
            return [Meteor.subscribe('league', this.params.league),
                    Meteor.subscribe('standings', this.params.league) ];

        },
        data: function () {
            return {theLeague: Leagues.findOne({league: this.params.league}),
                    theStandings: Standings.findOne()};
        }
    });
});

Server:

Meteor.publish('leagues', function(){
    console.log('all league names sent');
    return Leagues.find({}, {fields: {weeks: 0}});
});

Meteor.publish('league', function(theLeague){
    console.log('sending weeks detail for: ' + theLeague);
    return Leagues.find({league: theLeague});
});
Meteor.publish('standings', function(theLeague){
    console.log('standings: ' + theLeague);
    var file = Leagues.findOne({league: theLeague}).weeks[0].file;
    return Standings.find({file: file});
});

client:

Leagues = new Meteor.Collection('weeks');
Standings = new Meteor.Collection('details');
Meteor.subscribe('leagues');
Это было полезно?

Решение

There's work in progress in iron router to allow (and optimize) this (not immediately stopping the subscriptions when you route to another page). See the sub-manager branch.

But if you create the subscription apart from the waitOn call, I think the subscription is never stopped. For example, in the code below, the routes a and c will wait for the initialData to be received (which will be fetched directly when the user loads the page (even if it uses route b)), and the subscription for it will never stop, even if you leave, for example, route a. However, I don't think you can use this approach if you need to use some parameters in the route (you can probably fix something with setInterval, but it will be ugly).

var handleToDataIMostlyNeed = Meteor.subscribe('initialData')

Router.map(function(){
    this.route('a', {
        waitOn: function(){
            return handleToDataIMostlyNeed 
        }
    })
    this.route('b', {
        waitOn: function(){
            return [] // Wait for nothing.
        }
    })
    this.route('c', {
        waitOn: function(){
            return handleToDataIMostlyNeed 
        }
    })
})
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top