问题描述
我正在努力掌握 Meteor deps,为了具体说明我的用例是:
I'm struggling to get a handle on Meteor deps, in order to be specific my use case is:
2) 当集合改变时,我想在客户端运行一个 jQuery 函数
2) When collection is changed, I want to run a jQuery function on the client side
我认为 deps 是我要找的东西,但目前我才刚刚使用:
I think deps are what I'm looking for, but at the moment I have only just used:
Template.templateName.set () ->
return Set.find({})
...在反应性方面.
一个在每次更改时简单地将 console.log 记录在集合中的解决方案就足够了.
A solution which simply console.log's the collection each time it is changed would be more that enough.
推荐答案
使用上下文当然可以做你想做的事.它比[1]要复杂一点,但是如果您按照文档中的 deps 示例进行操作,则可以很容易地完成您想要的操作.类似的东西
It's certainly possible to do what you want using contexts. It's a little more complicated than it could be[1], but you can do what you want pretty easy if you follow the deps example from the docs. Something like
var setup = function() {
var context = new Meteor.deps.Context();
context.on_invalidate(function() {
setup();
});
context.run(function() {
Set.find({});
console.log('changed');
});
}
setup();
或者您可以制作一个不返回任何内容的帮助程序,以利用将为您设置的上下文:
Or you could make a helper that returns nothing to take advantage of the context that'll get setup for you:
Template.templateName.do_nothing ->
console.log 'changed'
Set.find({})
null
虽然这会强制 HTML 在您不希望刷新时刷新.
Although that will force the HTML to refresh when you might not want it to.
另一方面,您可能只需要 Collection.observe
.
On the other hand, you might just want Collection.observe
.
[1] 如果/当他们合并此拉取请求时,您可以这样做类似:
[1] If/when they merge this pull request, you could just do something like:
Meteor.deps.await(function() { return Set.find(); }, function() {
console.log('changed');
});
如果您想了解更多信息,可以查看 await
函数的源代码.
You can take a look at the source of the await
function if you want some more insight.
这篇关于Meteor Deps - 运行客户端功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!