Templates Meteor-如何知道数据是否已完全加载到模板中
我正在使用Meteor v0.9.1.1来填充一个小型即时消息应用程序。 这是我的Templates Meteor-如何知道数据是否已完全加载到模板中,templates,meteor,subscribe,Templates,Meteor,Subscribe,我正在使用Meteor v0.9.1.1来填充一个小型即时消息应用程序。 这是我的聊天室模板: <ul> {{#each messages}} <li class="msgLine"> <strong style="color: {{userTextColor userId}}"> {{userEmail}} </strong> {{userMs
聊天室模板:
<ul>
{{#each messages}}
<li class="msgLine">
<strong style="color: {{userTextColor userId}}">
{{userEmail}}
</strong>
{{userMsg}}
<span>{{createdAt}}</span>
</li>
{{/each}}
</ul>
但它不起作用。通过一步一步的调试,我发现Template.chat\u box.rendered
函数在DOM初始化后运行,而不是在从服务器加载所有消息时运行
所以问题是,我如何知道何时所有消息都已完全加载到模板中,以便在此之后我可以制作$chat.scrollTop($chat.height())代码>运行正常
这是我通过发布/订阅获取消息列表的方式:
Meteor.subscribe('rooms\u by\u id',this.params.\u id);
var currentRoomInfo=Rooms.find().fetch()[0];
如果(当前房间信息){
Meteor.subscribe('messages\u by\u room',this.params.\u id);
var_messages=messages.find().fetch();
如果(_消息){
返回{
roomId:currentRoomInfo.\u id,
roomTitle:currentRoomInfo.title.toUpperCase(),
消息:\ u消息
}
}
}
您可以使用下面介绍的技术:
但是,对于您的情况,您应该做一些改进:
Template.chat_box.rendered = function(){
Tracker.autorun(function(){
// every time "chatBoxSubscriptionCompleted" is changed, then closure is rerun
Session.get("chatBoxSubscriptionCompleted");
var $chat = $(".chatBox");
$chat.scrollTop($chat.height());
})
};
将onReady
回调传递给Meteor.subscribe
:
Meteor.subscribe(
"chatData",
param1,param2,
{
onReady:function(){
Session.set("chatBoxSubscriptionCompleted",true)
},
onError:function(){}
}
)
每当Session.get(“chatBoxSubscriptionCompleted”)
被更改时,就会执行Tracker.autorun中的闭包操作。这真的是唯一的方法吗?我本以为Meteor会有更好的方法,例如var my_data=Meteor.subscribe(“my_collection”);如果(my_data){…数据已加载,请执行某些操作…}