如何防止Meteor.js模板无需数据渲染

dim*_*kin 5 javascript jquery twitter-bootstrap meteor

有人可以帮我理解Meteors模板的行为吗?

我想在我的项目中实现一个通知系统,类似于Apples Growl.当记录写入数据库时​​,它使用简单的jQuery效果来显示消息.我简化了代码示例,以显示基本问题:

代码:

var Messages = new Meteor.Collection("messages");

if (Meteor.isClient) {
   Template.Notification.Message = function() {
     return Message.findOne({}, {sort: {seq: -1}});
   };

   Template.Notification.rendered = function() {
    $("#livebar").fadeIn(400).delay(1000).fadeOut(400);
   }
}
Run Code Online (Sandbox Code Playgroud)

模板:

<template name="Notification">
<div class="row">
   <div class="span6 alert alert-error" id="livebar" style="display:none;">
        {{Messages.text}}
   </div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)

如果呈现页面,则使用jQuery效果呈现空的不可见区域,然后系统加载反应数据源(Message)并再次呈现该区域!我试图阻止它渲染两次,但没有成功.错误似乎很容易解决,但我被困在这里.我将不胜感激任何帮助!

And*_*eas 3

{{> Notification}}您可以用块包围模板调用{{#if}}

{{#if has_notifications}}
  {{> Notifications}}
{{/if}}

//JS
Template.foo.has_notifications = function() {
  Message.find().count() > 0;
}    
Run Code Online (Sandbox Code Playgroud)

但由于数据不是一体到达的,因此可能会发生模板被渲染多次的情况。超时可以帮助你......