2017-02-23 40 views
0

the docs是否有必要停止訂閱與流星的幫傭級訂閱?

如果調用Meteor.subscribe反應的計算中,例如使用Tracker.autorun,當計算無效或停止訂閱將自動取消;

然後明確提到,不需要在autorun內停止訂閱。

這也是流星助手的情況嗎?我相信他們算作reactive computation,但我不完全確定!

EDIT

下面是代碼表示的情況的一個片段。 接下來的問題是:我需要做些什麼來阻止objectsSub或者是否全部自動排序?

<template name ="Foo"> 
{{#with myContext}} 
    {{#each objects}} 
    <!--Show stuff--> 
    {{/each}} 
{{/with}} 
</template> 

Template.Foo.onCreated(function(){ 
    this.subscribe('myContextSub'); 
}); 

Template.foo.helpers({ 
myContext(){ 
    return MyContextCollection.findOne(); 
}, 
objects(){ 
    Meteor.Subscribe('objectsSub',this.someContextAttribute); 
    return ObjectsCollection.find({}); 
}, 
}); 
+0

你爲什麼訂閱幫手?這對我來說似乎很奇怪。你能顯示你的代碼嗎? – zim

+0

我已經添加了一小段代碼。我在幫助程序中訂閱的原因是讓數據上下文可用,我將能夠在訂閱參數 – EugVal

+0

中使用好,我明白你在做什麼。我有一個想法,我會把答案。 – zim

回答

1

我不喜歡做一些有助益的事情,比如去服務器。一個幫助程序可以在模板處於活動狀態時多次調用,所以imho應該只會返回一個值。

在你的情況下,至少我會將訂閱綁定到模板,所以當模板被銷燬時訂閱就會消失。例如

Template.foo.helpers({ 
objects() { 
    Template.instance().subscribe('objectsSub',this.someContextAttribute); 
    return ObjectsCollection.find({}); 
}, 
}); 

更有可能,我會處理這個服務器端的「加盟」,當主集合(myContextSub)出版。但是這隻有在從屬集合(objectsSub)不被預期爲被動的時候。 (在發佈中,您可以在添加和更改的事件上設置偵聽器,並向發佈的對象添加額外的字段,即來自objectsSub的數據)。

如果objectsSub將被反應,那麼我可能會處理模板的onCreated()中的訂閱。在客戶端上,您可以在主集合上設置添加的偵聽器,然後在發佈主集合中的項目時訂閱相關的從集合。那麼幫手就可以像現在一樣簡單地執行find()。例如

Template.foo.onCreated(function() { 
    let self = this; 

    self.subscribe('myContextSub'); 

    let cursor = MyContextCollection.find(); 

    cursor.observe({ 
     added: function(newDocument) { 
      // loop through the objects on newDocument, pulling out the context attributes to subscribe one or more times... 
      self.subscribe('objectsSub', someContextAttribute[s]); 
     }, 
     changed: function(newDocument, oldDocument) { 
      // same as added 
     } 
    }); 
}); 

現在從屬助手可以更簡單:

Template.Foo.helpers({ 
myContext() { 
    return MyContextCollection.findOne(); 
}, 
objects() { 
    return ObjectsCollection.find({}); 
}, 
}); 

在這第二個例子,也許這是一個有點古怪的事情是我使用的是find()方法,而不是findOne()你」重新使用,以便以這種方式訪問​​聽衆。所以也許你需要檢查它在客戶端發佈或過濾的方式。

如果您想堅持使用findOne(),則應用相同的概念:數據返回後,您可以檢查它並訂閱從集合需要的內容。

1

好問題!

你是正確的,一個模板幫手實際上是一個反應計算。因此,根據文檔,應該遵循的是,您不必停止由幫助者開始的訂閱。但是你知道當你認爲會發生什麼......

所以我決定測試一下,以確保它在實際中是真的。根據我的測試,您的問題的答案是您不必停止由幫手啓動的訂閱。

如果您好奇,這裏是我的測試代碼(注意我在我的應用程序中使用了一個包含活動用戶列表的集合)。

<template name='main_template'> 
    <p>Number of Active Users: {{numUsers}}</p> 

    {{#if isNotDestroyed}} 
    <p>Number of Active Users (from sub-template): {{> sub_template}}</p> 
    {{/if}} 

    <a href="#" class="js-destroy">Destroy sub-template</a> 
</template> 

<template name='sub_template'> 
    {{numUsers}} 
</template> 


Template.main_template.onCreated(function() { 
    this.destory = new ReactiveVar(false); 
}); 

Template.main_template.helpers({ 
    numUsers: function() { 
    return ActiveUsers.find().count(); 
    }, 

    isNotDestroyed: function() { 
    return !Template.instance().destory.get(); 
    } 
}); 

Template.main_template.events({ 
    'click .js-destroy'(template, instance) { 
    console.log('setting destory'); 
    instance.destory.set(true); 
    }, 
}); 

Template.sub_template.onCreated(function() { 
    console.log("I was created!"); 
}); 

Template.sub_template.onDestroyed(function() { 
    console.log("I was destroyed!"); 
}); 

Template.sub_template.helpers({ 
    numUsers: function() { 
    Meteor.subscribe('activeUsers'); 
    return ActiveUsers.find().count(); 
    }, 
}); 

正如你所看到的,我訂閱了子模板內蒐集,但我在這兩個主要的模板和模板子計數的記錄數。在最初的運行中,兩個計數都會返回相同的值。但是,當我「銷燬」子模板(通過使用ReactiveVar實現)時,主模板中的計數變爲0.這意味着訂閱已停止並且本地集合已被清除。

最後一點,我完全同意@zim的推薦。除了他的建議,你還可以使用Meteor Publish Composite包來處理這個只有1個訂閱。

1

您可以使用此chrome extension來查看流星訂閱和取消訂閱的時間。 @ jordanwillis指出,您可能會看到它從幫手中的訂閱中取消訂閱。此外,我建議這server transform package做一個訂閱,而不是在一個幫手。

+0

這確實是Chrome的一個有用的擴展。 – zim