2016-07-04 60 views
0

假设我有流星/ NG2:凡来处理需要两个订阅解决的可变

this.subscribe('a',() => {this.allA = A.find();}); 
this.subscribe('b',() => {this.allB= B.find();}); 

和可变这就是说,

let x = *take the first A, do some calculation to get the B linked, return B* 

如果这样的逻辑被治请确保仅在订阅“a”和“b”得到解决时才处理此问题。

可能会使用区域,但我不是100%什么可能是最好的方式来做@Component。

PS:避免做-A-服务器端法:d

问候

+0

铁:路由器的waitOn提供这样的逻辑来等待多个订阅,我不使用铁路由器。 – ATX

回答

1

而是在您的订阅使用回调,回归订阅手柄,然后再检查他们ready()状态:

const subA = this.subscribe('a'); 
const subB = this.subscribe('b'); 
const that = this; 

Tracker.autorun(()=>{ 
    if (subA.ready()) that.allA = A.find(); 
    if (subB.ready()) that.allB = B.find(); 
    if (subA.ready() && subB.ready()) { 
    let x = ... //compute x 
    } 
}); 
+0

纠正我,如果我错了,但在你的例子中,没有行被阻止。因此,当达到.ready()时,结果很可能是错误的(在订阅之后直接调用)。只要没有准备好,你会把它放在setTimeout中吗? – ATX

+1

如你在答案中所做的那样将它封装在一个自动运行中将允许计算被动。 –

0

的解决方案似乎现在是将其包装在一个自动运行(MeteorComponent例如)

this.autorun(()=>{ 
    let s1 = this.subscribe('a',() => {this.allA = A.find();}); 
    let s2 = this.subscribe('b',() => {this.allB= B.find();}); 

    if(s1.ready() && s2.ready()) 
    ...