问题
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!
回答1:
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.
回答2:
First, userId is a function and userId() would return current user id. And you could check here to learn more detail of instance.subscribe
.
I think your problem may happen in getting runway._id
in Meteor.publish
Template.runway_panel.onCreated(function(){
var instance = this;
// instance subscribe data whose _id is runwayPanelId
instance.autorun(function(){
var dataContext = Template.currentData();
var subscription = instance.subscribe('runway_status', dataContext.runwayPanelId);
});
instance.status = function(){
return Runway_Status.find();
}
});
// publication
Meteor.publish('runway_status', function(runway){
// using Meteor.userId() to get current user id
if(Meteor.userId()){
//Retrieve the last know status for the given runway
return Runway_Status.find({runway: runway});
}
});
<template name = 'live'>
<div class = 'row'>
{{#each runways}}
<div class = 'col-md-2'>
{{> runway_panel runwayPanelId=_id}}
</div>
{{/each}}
</div>
</template>
来源:https://stackoverflow.com/questions/32690094/issue-with-subscriptions-on-multiple-instances-of-a-template