2016-11-25 85 views
0

我有以下模板:MeteorJS公佈超出範圍

<template name="reportsContent"> 
    <ul class="tabs"> 
     <li class="tabs-content" data-content="summary"> 
      <div class="tabs-content-wrapper"> 
       {{> reportsSummary }} 
      </div> 
     </li> 
     <li class="tabs-content" data-content="patients"> 
      <div class="tabs-content-wrapper"> 
       {{> reportsPatients }} 
      </div> 
     </li> 
    </ul> 
</template> 

<template name="reportsSumary"> 
    .... 
</template> 

<template name="reportsPatients"> 
    .... 
</template> 

我已經附上出版物的reportsSummary模板,但它似乎也延伸到reportsPatients模板。我不知道爲什麼,因爲我已經遵循了正確的方法來定義酒吧/潛艇(我想...)。

我知道它延伸到reportsPatients因爲我,如果我從一個reportsPatients助手返回Appointments.find()沒有訂閱的出版物,我得到這也是在reportsSummary

這裏的數據是我的刊物:

Meteor.publish('appointments.day.patients', function() { 

    var thisMonth = new RegExp(moment().format('MMM YYYY')); 

    return Appointments.find({ 
     date_created: { $regex: thisMonth } 
    }, { fields: { date_created: 1 } }); 
}); 

這是我的訂閱:

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

這並不是說我有什麼正在破壞任何功能本身。我只是擔心效率,當應用程序有一大堆數據,它必須通過篩選。我在這裏錯過了什麼嗎?

+0

出版物沒有範圍。如果數據發佈到客戶端,那麼在訂閱被撤消之前它將在任何地方都可用。 – MasterAM

回答

0

你是不是缺少什麼,那是流星的正常行爲。從服務器發佈的數據沒有範圍。一旦數據發佈到客戶端,所有的客戶端都可以訪問它們,即使在瀏覽器控制檯中運行的代碼也可以這樣做

這些公佈的數據將只在客戶端使用時認購其認購關閉被清除。就像在你的例子中,因爲你在reportsSummary模板中使用this.subscribe,所以當reportsSummary被銷燬時(當觸發onDestroyed事件時),這個訂閱將被關閉。

這是流星最佳實踐,始終把一個查詢中collection.find得到的文件。通過這種方式,您的操作明確了您期望得到的結果,並防止返回不需要的文檔。