流星数据库延迟 [英] meteor database latency

查看:42
本文介绍了流星数据库延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的meteor目录由2个文件组成:

My meteor directory is consists of 2 files:

social.js:

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

if (Meteor.isServer) {
}

if (Meteor.isClient) {
    Template.temple.messages = function() {
        return Messages.find();  
    };

    Meteor.startup(function () {
            console.log('console startup: ' + Messages.find().count());
    });

    console.log('console normal: ' + Messages.find().count());

    var ali = function() {
        console.log('console normal: ' + Messages.find().count());
    };

    setTimeout(function() {
        console.log('console normal: ' + Messages.find().count());
    }, 2000);
    // The callback function is not called on the following line:
    Meteor.subscribe('messages',function(){alert('ALERT!');});
}

social.html:

social.html:

<head>
<title>naber</title>
</head>

<body>
{{> temple}}
</body>

<template name = "temple">
{{#each messages}}
{{message}} <br />
{{/each}}
</template>

该集合仅在经过一段时间后才完全加载.如果我环绕 setTimeout,我只能看到文档的真实数量.怎样才能保证我的函数在数据库真正完全可用后执行?

The collection is fully loaded only after some time has passed. I can only see the true count of documents if I wrap around a setTimeout. What can I do to ensure that my functions are executed after the database is truly and fully available?

推荐答案

由于 Meteor 中反应性的工作方式,您总是需要针对服务器尚未将数据发送到客户端的位置设计应用程序状态.您可以将此称为加载"状态.

Because of how reactivity in Meteor works, there is always an application state you need to design for where the server has not yet sent the data to the client yet. You can call this the "loading" state.

Template 声明中,您必须始终检查您所依赖的数据是否可用.如果模板确实依赖于数据,则希望它首先呈现为空,然后在数据到达时进行更新,因为数据是反应式数据源.

Within Template declarations, you must always check whether data you depend on is available. If the template does depend on data, expect it first to render empty and then to update when data arrives, since the data is a reactive data source.

对于您自己的函数,最好以这样一种方式编写它们,即它们也依赖于响应式数据源,并使用诸如 Meteor.autorun 之类的东西来确保它们在此类数据源发生变化.

With your own functions, it's best to write them in such a way that they also depend on a reactive data source and use something like Meteor.autorun to make sure they're re-executed when such data sources change.

始终将您希望在页面加载后运行的代码放在 Meteor.startup 中,否则您的代码执行时甚至可能没有 Meteor 可用.

Always put code that you want to run after the page loads inside Meteor.startup, or you risk not even having Meteor available when your code executes.

以下是我将如何重写您的代码:

Here's how I would rewrite your code:

Messages = new Meteor.Collection("messages");
if (Meteor.isClient){
  Meteor.startup(function() {
    // anything you need to do, like subscriptions 
  });
  Template.temple.loading = function() {
    return Messages.find().count === 0 ? "Loading..." : "";
  }
  Template.temple.messages = function() {
    var messages = Messages.find();
    if (messages.count() > 0) return messages;
  }
}

<template name="messages">
  {{loading}}
  {{#each messages}}
    {{message}}<br>
  {{/messages}}
</template>

如果您只是在客户端上使用 Messages.find(),则实际上不需要 Meteor.subscribe 调用消息.

You don't actually need a Meteor.subscribe call to messages if you're just using Messages.find() on the client.

这篇关于流星数据库延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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