问题与模板的多个实例上的订阅 [英] Issue with subscriptions on multiple instances of a template

查看:83
本文介绍了问题与模板的多个实例上的订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景。我有一个包含#each循环的模板,并呈现特定模板的实例,并根据文档设置每个模板上的数据上下文。

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

< / template>

这是帮助者支持它:



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

这有效,我的问题如下。每个live_event_log实例都有一个模板级订阅,订阅了一个采用数据上下文的_id参数的发布,如下所示:

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

});

这是刊物:

<$ p $ (runway){

if(this.userId){
//检索给定的最后一个已知状态跑道

返回Runway_Status.find({跑道:跑道});
}

});

这是当它全部崩溃时,我在浏览器控制台上得到这个:

  [日志]排队任务异常:http:// localhost:3000 / client / views / live / runway_panel.js?4efaac87b39527d3dfd3d65a174520f9bce3c565:4:73 (meteor.js,第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

只要我将订阅行注释掉其他所有工作,我是否在这里错过了一些非常明显的内容?它可能与多个订阅到同一刊物有关吗?



谢谢! :)

解决方案

感谢Jeremy S.输入和一些睡眠后我终于在没有自动运行的情况下找到了一个夜班。所以这里是为后人:

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


this.data._id ,它现在被限制在最内层的函数中。你想要 instance.data._id (这是非反应性的,所以你不需要 autorun )或者 Template.currentData()(它是被动的)。

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

另请注意,在您的出版物中,您应该将其标记为 this.ready () if this.userId undefined 。但这不是错误的根源。


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!

解决方案

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.

这篇关于问题与模板的多个实例上的订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆