在回调中获取Meteor.subscribe

时间:2016-12-18 03:12:58

标签: javascript jquery reactjs meteor meteor-react

我有这段代码,

var switchValues =['one','two','three'];// if not in this array will use default
$scope.contactSelect = function(contact){
   var userId = contact.userId;
   // do something with the userId

   var switchValue = contact.switchProperty;
   // do something with the switchProperty
    if(switchValues.indexOf(switchValue ) === -1){
       // this will be default case with string "hello world" in the dom
       // do whatever you would have done in the jQuery
    }

}

我正在使用它,以便在回调中获取Meteor.subscribe。但它没有用。有没有其他方法来setState订阅?

1 个答案:

答案 0 :(得分:1)

看起来你很困惑。如果您想要.stop()订阅,您基本上只需要订阅对象。

.subscribe()的最后一个参数是函数时,它被解释为onReady()回调。

回调的this上下文不是订阅对象。它将是调用函数的拥有上下文,就像在Javascript中一样。在Meteor订阅onReady回调的情况下,这没有定义。

因此,在回调中,您不能仅依赖this,而只能依赖范围或闭包中的变量(例如self)。

如果你想在订阅准备就绪时做.find(),你应该做的是设置一个状态标志,表明它是,然后有一个函数取决于这个状态标志的值集合对象上的.find(),而不是订阅对象。

self.setState( { getOrgDetailReady: true } );

然后在其中一个生命周期方法中运行状态更改(哪一个取决于您的代码...),您执行:

if( this.state.getOrgDetailReady ) {
    let orgDetails = orgDetailCollection.find().fetch();
    ...
}
相关问题