Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

javascript - Issue with subscriptions on multiple instances of a template

Here is the scenario. I have a template that contains a #each loop and renders an instance of a particular template, setting the data context on each template as per the docs.

<template name = 'live'>
<div class = 'row'>
    {{#each runways}}
        <div class = 'col-md-2'>
            {{> runway_panel}}
        </div>
    {{/each}}
</div>

</template>

And this is the helper backing it:

Template.live.helpers({
    runways: function(){
        return Runway_Data.find();
    }
});

This works, my issue is as follows. Each live_event_log instance has a template level subscription that subscribes to a publication that takes the _id parameter of the data context, like so:

Template.runway_panel.onCreated(function(){
    var instance = this;
    instance.autorun(function(){
        var subscription = instance.subscribe('runway_status', this.data._id);
    });

    instance.status = function(){
        return Runway_Status.find();
    }

});

This is the publication:

Meteor.publish('runway_status', function(runway){ 

    if(this.userId){
        //Retrieve the last know status for the given runway

        return Runway_Status.find({runway: runway});  
    }

});

This is when it all falls apart, I get this on the browser console:

 [Log] Exception in queued task: http://localhost:3000/client/views/live/runway_panel.js?4efaac87b39527d3dfd3d65a174520f9bce3c565:4:73 (meteor.js, line 888)_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:16
http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:54
_withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:16

As soon as I comment out the subscription line everything else works, am I missing something really obvious here? Could it have something to do with multiple subscriptions to the same publication?

Thank you! :)

SOLUTION

Thanks to Jeremy S. input and some sleep after a night shift i've finally figured it out without an autorun. So here it goes for posterity:

Template.runway_panel.onCreated(function(){
    var self = this;
    self.subscribe('runway_status', this.data._id);
});

Should probably have tried getting some sleep before trying again!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is with this.data._id in your subscription, which right now is scoped to the innermost function. You want instance.data._id (which is nonreactive so you wouldn't need an autorun) or Template.currentData() (which is reactive).

Template.runway_panel.onCreated(function(){
    var instance = this;
    instance.autorun(function(){
        var data = Template.currentData();
        var subscription = instance.subscribe('runway_status', data._id);
    });
});

Also note that in your publication, you should mark it as this.ready() if this.userId is undefined. But that's not the source of the error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...