Meteor观察更改添加了对所有项目的服务器触发的回调 [英] Meteor observe changes added callback on server fires on all item
问题描述
Tracker.autorun(function(){
DATA.find()。observeChanges({
added:function(id,doc){
console.log(doc);
}
});
});
此代码正在服务器上调用。每次meteor服务器启动时,添加
函数将触发数据库中的每个项目。有没有办法让添加
回调仅在添加新项目时触发?
observeChanges
时,将为结果集中的每个文档调用div> 添加
。诀窍是在初始化期间忽略回调。我在回答这个问题时有一个扩展示例,但此代码应该适合你:
(function(){
var initializing = true;
DATA.find( ).observeChanges({
添加:function(id,doc){
if(!initializing){
console.log(doc);
}
}
});
initializing = false;
})();
请注意 Tracker.autorun 是一个仅限客户端的功能。在服务器上我认为它只执行一次。
Tracker.autorun(function() {
DATA.find().observeChanges({
added: function(id, doc) {
console.log(doc);
}
});
});
This code is being called on the server. Every time the meteor server starts, the added
function fires for every single item in the database. Is there a way to have the added
callback fire only when new items are added?
added
will be called for every document in the result set when observeChanges
is first run. The trick is to ignore the callback during this initialization period. I have an expanded example in my answer to this question, but this code should work for you:
(function() {
var initializing = true;
DATA.find().observeChanges({
added: function(id, doc) {
if (!initializing) {
console.log(doc);
}
}
});
initializing = false;
})();
Note that Tracker.autorun is a client-only function. On the server I think it only ever executes once.
这篇关于Meteor观察更改添加了对所有项目的服务器触发的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!