【问题标题】:Meteor publications/subscriptions not working as expectedMeteor 出版物/订阅未按预期工作
【发布时间】:2016-08-27 14:50:32
【问题描述】:

我有两份出版物。

第一个 pub 实现了搜索。尤其是This search

 /* publications.js */
Meteor.publish('patients.appointments.search', function (search) {
    check(search, Match.OneOf(String, null, undefined));

    var query = {},
    projection = { 
         limit: 10,
         sort: { 'profile.surname': 1 } };

    if (search) {
        var regex = new RegExp( search, 'i' );

        query = {
            $or: [
                {'profile.first_name': regex},
                {'profile.middle_name': regex},
                {'profile.surname': regex}
          ]
     };

    projection.limit = 20;
}
   return Patients.find(query, projection);
});

第二个基本是返回一些字段

/* publications.js */
 Meteor.publish('patients.appointments', function () {
   return Patients.find({}, {fields:  {'profile.first_name': 1,
                'profile.middle_name': 1,
                'profile.surname': 1});
});

我已经像这样订阅了每个出版物:

/* appointments.js */
Template.appointmentNewPatientSearch.onCreated(function () {
    var template = Template.instance();

    template.searchQuery = new ReactiveVar();
    template.searching = new ReactiveVar(false);

    template.autorun(function () {
       template.subscribe('patients.appointments.search', template.searchQuery.get(), function () {
          setTimeout(function () {
              template.searching.set(false);
          }, 300);
       });
    });
});


Template.appointmentNewPatientName.onCreated(function () {
    this.subscribe('patients.appointments');
});

所以这是我的问题:当我使用第二个订阅(appointments.patients)时,第一个不起作用。当我评论第二个订阅时,第一个订阅再次起作用。我不确定我在这里做错了什么。

【问题讨论】:

    标签: meteor meteor-publications


    【解决方案1】:

    这里的问题是您有两套针对同一个收藏的出版物。因此,当您在客户端中引用该集合时,现在可以指定它也必须引用哪个出版物。

    您可以做的是,集体发布所有数据,即您将需要的所有字段,然后使用客户端上的代码对其执行查询。

    另外,更好的方法是拥有两个模板。描述性代码:

    <template name="template1">
       //Code here
          {{> template2}}   //include template 2 here
    </template>
    
    <template name="template2">
         //Code for template 2
    </template>
    

    现在,为模板一订阅一份出版物,然后在那里做事。订阅模板 2 的第二个发布。 在主模板 (template1) 中使用把手语法 {{&gt; template2}} 包含 template2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多