Meteor/ng2: 在哪里处理需要解析两个订阅的变量
Meteor/ng2: Where to process a variable that requires two subscription to be resolved
假设我有
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:避免执行服务器端方法 :D
此致
不在您的订阅中使用回调,return 订阅句柄然后检查它们的 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
}
});
目前的解决方案似乎是将其包装在自动运行中(例如 Meteor 组件)
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())
...
假设我有
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:避免执行服务器端方法 :D
此致
不在您的订阅中使用回调,return 订阅句柄然后检查它们的 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
}
});
目前的解决方案似乎是将其包装在自动运行中(例如 Meteor 组件)
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())
...