[英]How to stop and restart collection observers in Meteor?
我希望能夠停止並重新啟動Meteor中我的集合上的觀察者。
想象一下,我有以下觀察者:
// Imagine some collection of Blog posts "Posts"
Posts.find().observe({
changed: notifySubscribedUsers
});
// function notifySubscribedUsers() { ... }
// is some function that will email everyone saying some post was updated
現在想象一下我想要更新很多帖子,但我不希望調用觀察者。 如何訪問觀察者,停止/暫停它們,然后重新啟動它們(在db作業完成后)?
TIA
觀察者返回一個句柄:
var handle = Posts.find().observe({
changed: notifySubscribedUsers
});
然后你可以用以下方法阻止它:
handle.stop()
傳統意義上的“暫停”它是不可能的,如果你想暫停它,你可以忽略它給你的數據。
要用一個整齊的包裝方法做到這一點,你可以做類似的事情:
var handle;
var start = function() {
if(handle) handle.stop();
var handle = Posts.find().observe({
changed: notifySubscribedUsers
});
}
var stop = function() { if(handle) handle.stop }
或者把它放在一個集合上:
// posts.js collection file
Posts.startObservers = function startObservers() {
Posts.observer = Posts.find().observe({
change: notifySubscribedUsers // or some other function
});
};
Posts.stopObservers = function stopObservers() {
if(Posts.observer) {
Posts.observer.stop(); // Call the stop
}
};
// Trigger Somewhere else in the code
Posts.stopObservers();
MyTool.doWorkOnPosts(); // Some contrived work on the Posts collection
Posts.startObservers();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.