当 mongo 集合更新时,Meteor 运行一个函数 [英] Meteor run a function when a mongo collection gets updated
问题描述
我正在尝试使用 Meteor 做一个简单的原型(我对 Meteor 很陌生).
I am trying to do a simple prototype using Meteor (I am very new to Meteor).
我在 isClient 块中有以下内容
I have the following in an isClient block
Template.imageList.helpers({
images: function() {
return Images.find({});
},
}
);
然后为了快速演示目的,我将使用meteor mongo和以下内容插入数据
Then for quick demo purposes I am inserting data with meteor mongo and the following
db.images.insert({ imgSrc: "http://mysite/img1.png", createdAt: new Date() })
这有效,我可以看到 ui 上反映的更改,但发生这种情况时我还需要运行客户端 javascript 函数.我一直在尝试 pub/sub 和 Tracker 之类的东西,但没有任何效果.
This works and I can see the change reflected on the ui but I need to also run a client side javascript function when this occurs. I have been trying things like pub/sub and Tracker but can't get anything to work.
任何指导都会很棒.
推荐答案
使用 observeChanges
:
Images.find().observeChanges({
added: function (id, fields) {
runFunction();
},
changed: function (id, fields) {
runFunction();
},
removed: function (id) {
runFunction();
}
});
请参阅此处了解更多信息:http://docs.meteor.com/#/full/observe_changes
See here for more: http://docs.meteor.com/#/full/observe_changes
这篇关于当 mongo 集合更新时,Meteor 运行一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!