如何等待 meteor.helper 中的计算,直到由于 template.autorun 中的依赖项更改而重新计算?
How to wait computation in meteor.helper until re-computation due to dependency change in template.autorun has been made?
当反应变量改变时我总是得到这个错误:
Exception in template helper: Error: $in needs an array
at Error (native)
at Object.ELEMENT_OPERATORS.$in.compileElementSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1827:15)
at http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1509:19
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22)
at operatorBranchedMatcher (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1489:5)
at compileValueSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1393:12)
at http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1372:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1355:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1332:12)
我怀疑罪魁祸首是w
(反应变量)
看看这个:
Template.cList.onCreated(function(){
var self = this;
self.d = new ReactiveVar()
self.w = new ReactiveVar()
self.autorun(function() {
var b = A.findOne(Pi());
if (b && b.array) {
var h = Subs.subscribe('B', b.array)
self.d.set(h.ready()) // is subscription ready?
self.w.set(b.array)
}
});
});
Template.cList.helpers({
cLists: function () {
if (Template.instance().d.get() && Template.instance().w.get()) { // run only when d and w is ready
return B.find({_id: {$in:Template.instance().w.get()}}, {fields: {title:1, src:1}});
}
}
});
让我们一步步分解...
- 第一次没有错误
- 当我更改页面时,以下依赖项将更改:
Pi()
- Auto运行 将重新运行.
- 因为
Pi()
改变,以下变量会改变:b && b.array && h && self.d.set(h.ready()) && self.w.set(b.array)
- 我怀疑
cList
是在页面更改后立即计算的,没有等待自动 运行 重新计算完成,这就是抛出错误的原因。
- 在自动运行完成重新计算依赖变化后,
cLists
根据依赖变化的变化显示正确的列表。用户界面确实没有问题。但是这个警告看起来很脏。
我想避免上面的警告是在依赖项更改时等待 cList。我怎么做?我如何等待 meteor.helper 中的计算,直到由于 template.autorun 中的依赖项更改而重新计算?
罪魁祸首是来自 meteorhacks:subs-manager
的 ready callback
...用模板订阅更改它会避免错误...
Template.cList.onCreated(function(){
var self = this;
self.d = new ReactiveVar()
self.w = new ReactiveVar()
self.autorun(function() {
var b = A.findOne(Pi());
if (b && b.array) {
self.subscribe('B', b.array)
self.w.set(b.array)
}
});
});
Template.cList.helpers({
cLists: function () {
if (Template.instance().subscriptionsReady() && Template.instance().w.get()) { // run only when subscription and w is ready
return B.find({_id: {$in:Template.instance().w.get()}}, {fields: {title:1, src:1}});
}
}
});
当反应变量改变时我总是得到这个错误:
Exception in template helper: Error: $in needs an array
at Error (native)
at Object.ELEMENT_OPERATORS.$in.compileElementSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1827:15)
at http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1509:19
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22)
at operatorBranchedMatcher (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1489:5)
at compileValueSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1393:12)
at http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1372:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:157:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1355:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?cdf1a26cf7719fa9471a8017c3defd5aea812727:1332:12)
我怀疑罪魁祸首是w
(反应变量)
看看这个:
Template.cList.onCreated(function(){
var self = this;
self.d = new ReactiveVar()
self.w = new ReactiveVar()
self.autorun(function() {
var b = A.findOne(Pi());
if (b && b.array) {
var h = Subs.subscribe('B', b.array)
self.d.set(h.ready()) // is subscription ready?
self.w.set(b.array)
}
});
});
Template.cList.helpers({
cLists: function () {
if (Template.instance().d.get() && Template.instance().w.get()) { // run only when d and w is ready
return B.find({_id: {$in:Template.instance().w.get()}}, {fields: {title:1, src:1}});
}
}
});
让我们一步步分解...
- 第一次没有错误
- 当我更改页面时,以下依赖项将更改:
Pi()
- Auto运行 将重新运行.
- 因为
Pi()
改变,以下变量会改变:b && b.array && h && self.d.set(h.ready()) && self.w.set(b.array)
- 我怀疑
cList
是在页面更改后立即计算的,没有等待自动 运行 重新计算完成,这就是抛出错误的原因。 - 在自动运行完成重新计算依赖变化后,
cLists
根据依赖变化的变化显示正确的列表。用户界面确实没有问题。但是这个警告看起来很脏。
我想避免上面的警告是在依赖项更改时等待 cList。我怎么做?我如何等待 meteor.helper 中的计算,直到由于 template.autorun 中的依赖项更改而重新计算?
罪魁祸首是来自 meteorhacks:subs-manager
的 ready callback
...用模板订阅更改它会避免错误...
Template.cList.onCreated(function(){
var self = this;
self.d = new ReactiveVar()
self.w = new ReactiveVar()
self.autorun(function() {
var b = A.findOne(Pi());
if (b && b.array) {
self.subscribe('B', b.array)
self.w.set(b.array)
}
});
});
Template.cList.helpers({
cLists: function () {
if (Template.instance().subscriptionsReady() && Template.instance().w.get()) { // run only when subscription and w is ready
return B.find({_id: {$in:Template.instance().w.get()}}, {fields: {title:1, src:1}});
}
}
});