在下一步之前等待流星收集完成 [英] Waiting for meteor collection to finish before next step

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

问题描述

我有一个应该显示一些数据的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:无法读取未定义的属性'date'

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模板声明中,我们使用封装外部模板来处理模板级订阅模式。
我们在 onCreated 生命周期事件中订阅该出版物,我们使用特殊的反应助手 Template.subscriptionsReady 只有在订阅准备好后才能呈现 svgTemplate (数据在浏览器中可用)。
此时,我们可以安全地查询 svgTemplate 中的 Pushups 集合onRendered 生命周期事件,因为我们确保数据进入客户端:

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.

将包添加到项目中:

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(/*...*/);
  }
});

使用这段简单的代码,您将得到您想要的东西以及许多附加功能。
你可以看看铁路由器指南,它详细解释了这些功能。

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

编辑18/3/2015:重新设计答案,因为它包含过时的材料,但仍然收到了赞成票。

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

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

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