流星客户端异步模式/如何实现waitOn为订阅W的回调列表 [英] meteor client async pattern / how to implement a waitOn for a list of subscriptions w callbacks

查看:115
本文介绍了流星客户端异步模式/如何实现waitOn为订阅W的回调列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于种种原因我写不使用IronRouter,但实现某种类似的逻辑的应用程序。一位正在等待的订阅列表做好准备。

for various reasons I'm writing an app that doesn't use IronRouter, but has to implement some similar logic. One is waiting for a list of subscriptions to be ready.

由于这是流星的异步调用客户端,都有些什么技巧这样做呢?

Since this is an async call client side in meteor, what are some techniques for doing this?

如果我想有像潜艇的列表:

If I want to have a list of subs like:

  sublist = [
    PubSubMan.subscribe("Players"),
    PubSubMan.subscribe("Stuff")
  ]

然后与应用程序的其余部分启动,一旦他们都。就绪()=真

什么是做到这一点的好办法?

what is a good way to do this?

我不是很了解的wait()方法是如何在IR <一实施href=\"https://github.com/EventedMind/iron-router/blob/4b3388d7540af50be90dad5549938fcf21a82b39/lib/route_controller_client.js#L283\"相对=nofollow>来源$ C ​​$ C这里

I can't quite understand how the wait() method is implemented in the IR source code here

这似乎是一个aysnc.js型的情况,在这里我要调用的方法列表,继续当他们的回调完成理想的情况下,却诉诸节点风格的图案似乎是流星有点笨重。我看着wrapAsync和 meteorhacks异步utils的但似乎主要是用于服务器的方法和包装NPM包

This seems an ideal case for an aysnc.js type situation, where I want to call a list of methods and continue when their callbacks are done, but resorting to node style patterns seems a bit clunky for meteor. I looked at wrapAsync and meteorhacks async utils but that seems mostly for server methods and wrapping NPM packages.

如果我能总结就绪()值的列表,然后创建一个跟踪DEPS如果/当这一数额改变,将火...?不过不太清楚怎么做,要么。

If I could sum the list of ready() values and then create a Tracker deps that would fire if/when that sum changed... ? but not quite sure how to do that either.

由于完成后每个订阅触发一个回调,我想我可以用一个计数器来跟踪回调时被解雇,并保持一个计数器来检查==数组的长度,但再次证明似乎有点不雅。

Since each of the subscriptions fires a callback when done, i guess i could use a counter to track when callbacks are fired and keep a counter to check == length of the array, but again that seems kind of inelegant.

编辑:
这不是一个理想的解决方案,但在下面的作品。但我仍然认为我失去了一个更优雅的方式。

This isn't an ideal solution but the below works. But I still think I'm missing a more elegant method.

  subList = [
    PubSubMan.subscribe("Players"),
    PubSubMan.subscribe("Stuff" )
  ]

  Tracker.autorun (c) =>
    subReady = _.filter subList, (item) ->
      return item.ready()
    allDone = (subList.length == subReady.length)
    console.log("subs status: #{subReady.length} / #{subList.length} = ready: #{allDone}")
    if allDone
      c.stop()
      startMainLoop()

有关这个问题上tracker.autorun如何挑选其计算的依赖
<一href=\"http://stackoverflow.com/questions/26354582/how-does-tracker-autorun-pick-out-its-computation\">how不Tracker.autorun挑出它的计算?

推荐答案

我不知道你的 startMainLoop 像,但这里的,可能为你工作的方法。创建一个新的模板,只是检查是否所有的订阅已经准备好;如果他们是它呈现真实主模板,而如果它们不那么它呈现一个装载模板

I'm not sure what your startMainLoop is like, but here's an approach that might work for you. Create a new template which just checks if all the subscriptions are ready; if they are it renders the real main template, and if they aren't then it renders a loading template.

<template name="subscriptionsReadyCheck">
  {{#if allSubsAreReady}}
    {{> mainTemplate}}
  {{else}}
    {{> loadingTemplate}}
  {{/if}}
</template>

subList = [...]

Template.subscriptionsReadyCheck.helpers {
  allSubsAreReady: -> _.every(subList, (sub) -> sub.ready())
}

这假设,当直到页面被关闭的页面加载,并保持周围的订阅创建。如果你需要在模板呈现它只是创建,并停止订阅当模板被破坏,你可以将它们存储在模板实例:

This assumes that the subscriptions are created when the page loads and stays around until the page is closed. If you need subscriptions which are only created when the template is rendered and are stopped when the template is destroyed, you can store them on the template instance:

Template.subscriptionsReadyCheck.created = ->
  @subList = [...]

Template.subscriptionReadyCheck.destroyed ->
  for sub in @subList
    sub.stop()

Template.subscriptionsReadyCheck.helpers {
  allSubsAreReady: -> _.every(Template.instance().subList, (sub) -> sub.ready())
}

这篇关于流星客户端异步模式/如何实现waitOn为订阅W的回调列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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