【问题标题】:meteor iron:router Blaze.ReactiveVar calling multiple times陨铁:路由器 Blaze.ReactiveVar 多次调用
【发布时间】:2015-02-22 17:48:33
【问题描述】:

我有一条路线,我打了很多次。我必须订阅两个集合才能拥有所有数据,这是一个快照:

var one = new Blaze.ReactiveVar(false);
var two = new Blaze.ReactiveVar(false);
this.route('stopIndex', {
  path: '/stop/:section/:stop_id',
  waitOn: function() {
      Meteor.call('getTripIdsForStop', {
        stop_id: this.params.stop_id,
        from: fromNow(),
        to: toMax(),
        db: prefix
      }, function(err, ids) {
        DEBUG && console.log('TRIP_IDS:', ids);
        Meteor.subscribe(prefix + '_trips', {
          trip_id: {$in: ids}
        }, function() {
          one.set(true);
        });
        Meteor.subscribe(prefix + '_stop_times', {
          trip_id: {$in: ids}
        }, function() {
          two.set(true);
        });
      });
      return [
        function () { return one.get(); },
        function () { return two.get(); }
      ];
    },

我第一次调用路由时,一切正常。第二次,onetwo 变量已设置为 true,因此 waitOn 不会等待,我会在模板上收到 no data 消息几秒钟,直到集合响应。我试过把waitOk 方法的第一行放在上面:

one.set(false);
two.set(false);

但这会使waitOn 永远等待。我做错了什么或错过了什么?感谢您的帮助。

【问题讨论】:

    标签: meteor iron-router meteor-blaze


    【解决方案1】:

    我是这样解决的:

    Router.onStop(function() {
      one.set(false);
      two.set(false);
    });
    

    这会使ReactiveVars 无效并等待。我还将所有代码从waitOn 移动到data。现在waitOn 是这样的:

    return [
        function () { return one.get(); },
        function () { return two.get(); }
      ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 2014-12-24
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2014-10-09
      • 1970-01-01
      相关资源
      最近更新 更多