2016-08-27 42 views
1

我有两个出版物。流星的出版物/订阅不能按预期工作

第一个酒吧执行搜索。特别是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),第一个不起作用。当我评论第二次订阅时,第一次订阅会再次生效。我不确定我在这里做错了什么。

回答

0

这里的问题是你有两套同一集合的出版物。所以,当你在客户端引用集合时,现在有办法指定它必须引用哪一个出版物。

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

或者,更好的方法是有两个模板。一个描述性的代码:

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

<template name="template2"> 
    //Code for template 2 
</template> 

现在,订阅一个发布模板之一,并在那里做的东西。订阅第二版到模板2. 在主模板(template1)中包含template2其中使用句柄语法{{> template2}}