【问题标题】:Using collection inside Iron Router's onBeforeAction在 Iron Router 的 onBeforeAction 中使用集合
【发布时间】:2015-02-28 19:07:18
【问题描述】:

我有一个名为 Tabs 的集合和一个以 tabId 作为其路由参数的动态路由。选项卡具有布尔属性“已发布”,如果用户访问相应选项卡记录的“已发布”为 false 的路由,我想将用户重定向回根 url。

为此,我想我可以在 onBeforeAction 中获取记录并检查它,但由于某种原因,从订阅返回的游标未定义。

路线:

viewTabController = RouteController.extend({
  layoutTemplate: 'storeLayout',
  subscriptions: function() {
    var tabId    = this.params.tabId;
    var sourceId = readCookie(Purchases.COOKIE_IDENTIFIER(tabId));

    this.sub     = Meteor.subscribe("itemData", tabId, sourceId);
  },
  data: function() {
    var tabId = this.params.tabId;
    return {
      tab:      Tabs.findOne(tabId),
      ready:    this.sub.ready
    };
  },
  onBeforeAction: function() {
    if (this.sub.ready) {
      var tab      = Tabs.findOne(); <------------ undefined
      var purchase = Purchases.findOne(); <-------------- undefined

      console.log(tab, purchase);

      if (!_.isUndefined(purchase)) {
        console.log('go receipt');
        // Router.go('receipt', { purchaseId: purchase._id });
      } else if (!tab.isPublished()) {
        console.log('is not Published');
      } else {
        console.log('view tab');
      }
    }
    this.next();
  },
  onAfterAction: function() {
    SEO.set({
      title: 'View Tab | ' + SEO.settings.title
    });
  },
  fastRender: true
});

Router.route('viewTab', {
  path: '/tabs/:tabId',
  controller: viewTabController
});

使用 publishComposite 发布:

/*
 * Returns tab matching ID
 */
Meteor.publishComposite("itemData", function(tabId, sourceId) {
  check(tabId, String);

  return {
    find: function() {
      return Tabs.find({ _id: tabId }, { fields: { downloadUrl: 0, s3key: 0, createdAt: 0 } });
    },
    children: [
      {
        find: function(tab) {
          return Users.find({ _id: tab.userId }, { fields: { username: 1 } });
        }
      },
      {
        find: function(tab) {
          return Purchases.find({ tabId: tabId, sourceId: sourceId });
        }
      }
    ]
  };
});

这是错误的方法还是我遗漏了什么?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    这可能不是答案,但我希望它能进一步帮助。

    问题是 Tabs & Purchases.FindOne() 在应该使用的时候还没有准备好,please read this great post for clarification

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多