2016-08-27 44 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}}