等待流星收集完成后再进行下一步 [英] Waiting for meteor collection to finish before next step

查看:18
本文介绍了等待流星收集完成后再进行下一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该显示一些数据的 Meteor 模板.

I have a Meteor template that should be displaying some data.

Template.svg_template.rendered = function () {
  dataset_collection = Pushups.find({},{fields: { date:1, data:1 }}, {sort: {date: -1}}).fetch();

  a = moment(dataset_collection[0].date, "YYYY/M/D");
  //more code follows that is also dependent on the collection being completely loaded
};

有时它有效,有时我会收到此错误:

Sometimes it works, sometimes I get this error:

Deps afterFlush 函数的异常:TypeError:无法读取未定义的属性日期"

Exception from Deps afterFlush function: TypeError: Cannot read property 'date' of undefined

我没有在任何情况下使用 Deps.据我了解,该集合在完全加载完成之前被引用.

I'm not using Deps in any context. As I understand it, the collection is being referenced before it is completely finished loading.

因此,我想弄清楚如何简单地说等到找到集合再继续".应该很简单,但找不到更新的解决方案.

I therefore would like to figure out how to simply say "wait until the collection is found before moving on." Should be straightforward, but can't find an updated solution.

推荐答案

您说得对,您应该确保在正确加载数据后执行依赖于获取客户端订阅集合内容的代码.

You are right, you should ensure that code depending on fetching the content of a client-side subscribed collection is executed AFTER the data is properly loaded.

您可以使用 Meteor 1.0.4 中引入的新模式实现此目的:https://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe

You can achieve this using a new pattern introduced in Meteor 1.0.4 : https://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe

client/views/svg/svg.js

Template.outer.onCreated(function(){
  // subscribe to the publication responsible for sending the Pushups
  // documents down to the client
  this.subscribe("pushupsPub");
});

client/views/svg/svg.html

<template name="outer">
  {{#if Template.subscriptionsReady}}
    {{> svgTemplate}}
  {{else}}
    Loading...
  {{/if}}
</template>

在 Spacebars 模板声明中,我们使用封装的 outer 模板来处理模板级订阅模式.我们在 onCreated 生命周期事件中订阅发布,我们使用特殊的响应式助手 Template.subscriptionsReady 只在订阅后渲染 svgTemplate已准备就绪(浏览器中的数据可用).此时,我们可以安全地查询 svgTemplate onRendered 生命周期事件中的 Pushups 集合,因为我们确保数据已到达客户端:

In the Spacebars template declaration, we use an encapsulating outer template to handle the template level subscription pattern. We subscribe to the publication in the onCreated lifecycle event, and we use the special reactive helper Template.subscriptionsReady to only render the svgTemplate once the subscription is ready (data is available in the browser). At this point, we can safely query the Pushups collection in the svgTemplate onRendered lifecycle event because we made sure data made its way to the client :

Template.svgTemplate.onRendered(function(){
  console.log(Pushups.find().fetch());
});

或者,您可以使用 iron:router (https://github.com/Iron-meteor/iron-router),它提供了另一种设计模式来解决这个常见的 Meteor 相关问题,将订阅处理转移到路由级别而不是模板级别.

Alternatively, you could use the iron:router (https://github.com/iron-meteor/iron-router), which provides another design pattern to achieve this common Meteor related issue, moving subscription handling at the route level instead of template level.

将包添加到您的项目中:

Add the package to your project :

meteor add iron:router

lib/router.js

Router.route("/svg", {
  name: "svg",
  template: "svgTemplate",
  waitOn: function(){
    // waitOn makes sure that this publication is ready before rendering your template
    return Meteor.subscribe("publication");
  },
  data: function(){
    // this will be used as the current data context in your template
    return Pushups.find(/*...*/);
  }
});

使用这段简单的代码,您将获得所需的内容以及许多附加功能.您可以查看 Iron Router 指南,其中详细介绍了这些功能.

Using this simple piece of code you'll get what you want plus a lot of added functionalities. You can have a look at the Iron Router guide which explains in great details these features.

https://github.com/iron-meteor/iron-router/blob/devel/Guide.md

EDIT 18/3/2015:重新修改了答案,因为它包含过时的材料,但仍然收到了赞成票.

EDIT 18/3/2015 : reworked the answer because it contained outdated material and still received upvotes nonetheless.

这篇关于等待流星收集完成后再进行下一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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