【问题标题】:how to properly use meteor's ironRouter waitOn?如何正确使用流星的ironRouter waitOn?
【发布时间】:2016-03-03 11:26:58
【问题描述】:

我的 mongo 集合中有大约 6000 个文档,我需要在应用程序启动时将它们加载到流星客户端中。 在我的路线中(位于应用程序/客户端下),我有这个:

Router.map(function() {
    this.route('home', {
        path: '/',
        template: 'dashboardWrapper',
        waitOn: function() {
            return Meteor.subscribe('nasdaq');
        },
        cache: true
    });
});

我的 dashboardWrapper 模板如下所示:

<template name="dashboardWrapper">
    {{#if Template.subscriptionsReady}}
        {{> dashboard}}
    {{/if}}
</template>

My dashboard template looks like this:

<template name="dashboard">
    <div class="container">
        <h2>Priority - 1 Incidents Over Time</h2>

        <div class="row">
            <div id="yearly-bubble-chart" class="dc-chart">
                <strong>Yearly Performance</strong> (radius: fluctuation/index ratio, color: gain/loss)
            </div>
        </div>

        <div class="row">
            <div id="gain-loss-chart">
                <strong>Days by Gain/Loss</strong>
                <a class="reset" href="javascript:gainOrLossChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>

                <div class="clearfix"></div>
            </div>

            <div id="quarter-chart">
                <strong>Quarters</strong>
                <a class="reset" href="javascript:quarterChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>

                <div class="clearfix"></div>
            </div>

            <div id="day-of-week-chart">
                <strong>Day of Week</strong>
                <a class="reset" href="javascript:dayOfWeekChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>

                <div class="clearfix"></div>
            </div>

            <div id="fluctuation-chart">
                <strong>Days by Fluctuation(%)</strong>
                <span class="reset" style="display: none;">range: <span class="filter"></span></span>
                <a class="reset" href="javascript:fluctuationChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>

                <div class="clearfix"></div>
            </div>
        </div>

        <div class="row">
            <div id="monthly-move-chart">
                <strong>Monthly Index Abs Move & Volume/500,000 Chart</strong>
                <span class="reset" style="display: none;">range: <span class="filter"></span></span>
                <a class="reset" href="javascript:moveChart.filterAll();volumeChart.filterAll();dc.redrawAll();"
                   style="display: none;">reset</a>

                <div class="clearfix"></div>
            </div>
        </div>

        <div class="row">
            <div id="monthly-volume-chart">
            </div>
            <p class="muted pull-right" style="margin-right: 15px;">select a time range to zoom in</p>
        </div>

        <div class="row">
            <div>
                <div class="dc-data-count">
                    <span class="filter-count"></span> selected out of <span class="total-count"></span> records | <a
                        href="javascript:dc.filterAll(); dc.renderAll();">Reset All</a>
                </div>
            </div>
            <table class="table table-hover dc-data-table">
            </table>
        </div>

        <div class="clearfix"></div>

    </div>
</template>

Meteor.client 的相关部分如下所示:

if (Meteor.isClient) {

  var ndx,data;

  Template.dashboardWrapper.onCreated( function() {
    var template = this;
    template.subscribe("nasdaq");
  });

  Template.dashboard.onCreated( function() {
          data = Nasdaq.find().fetch();
          ndx = crossfilter(data);
  });

    Template.dashboard.onRendered(function(){

        var self = this;
        self.subscribe("nasdaq", function() {
            self.autorun(function() {
                data = Nasdaq.find().fetch();
            });
        });

我对此的期望是仪表板模板等到纳斯达克集合中的所有数据加载完毕。 发生的事情绝对没有——没有数据也没有错误。

如果我将ironRounter 全部删除并刷新,我可以获得几十条记录(总共6000 条)。 有没有办法可靠地强制应用程序等到每个文档加载完毕?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    在加载当前模板之前尝试订阅,可能会起作用。

    Router.route('/dashboardWrapper/:_id', {
        name: 'dashboardWrapper',
        waitOn: function () {
            return [
                Meteor.subscribe('nasdaq')
            ];
        },
        data: function () {
            return Nasdaq.findOne(this.params._id);
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 2014-11-17
      相关资源
      最近更新 更多