【问题标题】:In meteor, how to wait for subscriptions and access them in template `rendered` function?在流星中,如何等待订阅并在模板`rendered`函数中访问它们?
【发布时间】:2015-11-17 15:31:54
【问题描述】:

我在我的应用程序中使用FlowRouter,并且是 Meteor 的新手。我已经像这样订阅了我的收藏:

Template.PanelEditAbout.onCreated(function() {
    var self = this;
    self.autorun(function() {
        self.subscribe('pages', 'about');
    });
});

我正在尝试在rendered 函数中使用订阅,但它不起作用:

Template.PanelEditAbout.rendered = function() {
    page = Pages.findOne({
        slug: 'about'
    });
}

如果我正确,我必须等待订阅可用。我怎样才能做到这一点?我还需要在准备就绪时添加加载消息(或微调器)。我知道如何在 IronRouter 中执行此操作,但不知道 FlowRouter。

【问题讨论】:

    标签: meteor flow-router


    【解决方案1】:

    永远不要订阅onRendered,首先。试试下面的:

    Template.PanelEditAbout.onCreated(function() {
      this.subscribe('pages', 'about');
    });
    
    Template.PanelEditAbout.onRendered(function() {
      let page = {};
    
      this.autorun(() => {
        if (this.subscriptionsReady()) {
          console.log('subs ready');
          page = Pages.findOne({
            slug: 'about'
          });
        }
    
        console.log(page);
      });
    });
    

    【讨论】:

      【解决方案2】:

      如果它是您出版物的名称,您可以在订阅页面期间使用 onReady。

      Template.PanelEditAbout.rendered = function() {
        Meteor.subscribe("page", Yourslug,{
          onReady: function () { console.log("onReady And the Itemns actually Arrive",     arguments); },
          onError: function () { console.log("onError", arguments); }
        });
      };
      

      控制台日志只是一个例子。

      【讨论】:

      • 如果您谈到像实时聊天一样加载消息,我认为您必须使用 reivars 和助手。
      • 一切正常,但是当我添加加载消息时,CKEDITOR 函数找不到文本区域
      • 好的,在另一个答案中,他是正确的,他在 onCreated 中进行订阅并等待,并使用帮助器格式化您的数据,然后您将其放入您的 html 页面 。我不完全确定,但试试看。
      • 太棒了!这正是我需要做一个递归订阅模型。在加载数据 B 和 C 之前,我必须确保数据 A 可用,最后是依赖于 C 的 D。
      猜你喜欢
      • 1970-01-01
      • 2021-06-08
      • 2018-04-27
      • 2015-12-12
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多