2015-07-12 74 views

回答

0

首先,我想你应该重新考虑是否要发布那么多文件:它们全部转移到每个客户端并造成大流量。另外我怀疑如果你“暂停”发布服务器不会耗尽内存。

如果确实需要这样做,可以通过简单的方法暂停发布:将订阅中的布尔参数传递给发布,并在客户端操作触发批量插入时反应性地重新运行订阅。听起来很复杂,很简单:

// on the client 
Tracker.autorun(function() { 
    // reactively depend on event causing batch insert 
    var doPublish = reactiveVarTrueIfBatchInsert.get(); 
    Meteor.subscribe('myPub', doPublish, function() { // ... }); 
}); 
... 
// inside some event 
reactiveVarTrueIfBatchInsert.set(true); 
Meteor.defer(function() { 
    Meteor.call('batchInsertMethod', function() { 
     reactiveVarTrueIfBatchInsert.set(false); 
    }); 
}); 

// on the server 
Meteor.publish('myPub', function() { 
    var doPublish = arguments[0]; 
    var result = []; 
    if(doPublish) result = myDocs.find({}); 
    return result; 
}); 

的核心思想是,reactiveVarTrueIfBatchInsertreactive variable这是真的,当且仅当您所在批量插入。 注意:使用Meteor.defer的包装是非常重要的,因为没有它,被动变量在方法调用之前没有时间影响订阅。