使用文本输入,我将文档添加到集合对象(每个对象都包含文本输入的文本)和包含Meteor.default_connection._lastSessionId的sess参数(如:Links.insert({sess:Meteor.default_connection._lastSessionId,youtube_link:url}); )
在myapp.html中,我有:
<template name="list_of_links">
<ul id="item-list">
{{#each my_playlist}}
{{> link_item}}
{{/each}}
</ul>
</template>
<template name="link_item">
<li class="link">
<div class="link-title">{{youtube_link}} {{sess}}</div>
</li>
</template>在myapp.js中
在isClient下,我有:
Meteor.subscribe("links");
Template.list_of_links.my_playlist = function () {
//return Links.find({sess: Meteor.default_connection._lastSessionId});
return Links.find();
};在isServer下,我有:
Meteor.publish("links", function() {
//return Links.find({sess:Meteor.default_connection._lastSessionId});
return Links.find();
});注意注释掉的行。没有它们(就像现在一样),应用程序会在#each循环中打印db中的所有元素。使用现在注释的行(这是所需的行为),我希望能够在列表中显示当前浏览器会话中的元素,但是即使调用:Links.find({sess:Meteor.default_connection._lastSessionId}).fetch()给出了所需的输出,也看不到显示任何内容.
是什么导致了这一切,我解决了吗?
发布于 2013-11-16 06:05:27
听起来至少有两个问题:
1)我认为Meteor.default_connection在服务器发布功能中不可用
2)游标可以获取所期望的值,但将游标返回到模板时,则不会显示这些值,这听起来就像一个bug。请记住,在获取值以检查值和在模板中返回值之间,需要在游标上调用.rewind()。
要解决第一个问题,客户端应该按以下方式订阅:
Deps.autorun( function(){
if ( Meteor.status().connected )
Meteor.subscribe( "links", Meteor.default_connection._lastSessionId);
});然后像这样在服务器上发布:
Meteor.publish("links", function( sess ) {
return Links.find({sess: sess}); //each client will only have links with that _lastSessionId
});然后模板助手变成:
Template.list_of_links.my_playlist = function () {
return Links.find(); //all links client has but client only has links with their _lastSessionId
};我不知道这是否也会解决第二个问题,因为这将是一个令人惊讶的错误。
-更新代码,以便向Deps.autorun中添加一个反应性变量。否则,自动运行程序只能运行一次。基于评论的here
https://stackoverflow.com/questions/20015001
复制相似问题