2013-03-19 52 views
0

我想通過Meteor.autorun更改Collection(GraphData)訂閱限制客戶端緩存。但我注意到,服務器端只在會話(瀏覽器GUI)更改後才發佈數據。那是對的嗎?如何控制流星會話更新和數據庫訂閱延遲?

在客戶端,我有以下的CoffeeScript代碼:

Meteor.startup -> 
    Meteor.autorun() -> 
    Meteor.subscribe 'graphdata', Session.get 'graph_name' 

在功能,draw_graph,我有

Session.set 'graph_sub', false  
Session.set 'graph_name', item_name 
ready = Session.get 'graph_sub' 
while !(ready)  
    Meteor.setTimeout (ready = Session.get 'graph_sub'), 1000 
Do something with the GraphData subscription 

在服務器端,我有

Meteor.startup -> 
    Meteor.publish 'graphdata', (name) -> 
    if name? 
     GraphData.find({name: name}) 
     Session.set 'graph_sub', true 

我期待在Session.set'graph_name',item_name之後觸發服務器端發佈,但我注意到了我陷入了while循環。

我的理解是否正確?無論如何強迫會話變量更改得到服務器端沒有會話更改注意到?

+0

循環'ready = false; setTimeout(( - > ready = true),1000)直到ready就不會像你想的那樣工作,它會導致內部的'( - > ready = true)'被稱爲bazillion times。 (編輯:把它寫成一行代碼,以適應這種單行註釋格式;編譯成JS以更清楚地看到問題。) – 2014-06-04 09:39:33

+0

很明顯,你不會在你提供的單行版本中工作,因爲準備不會從不改變。在我的代碼中,我希望一旦服務端將graph_sub設置爲true,就可以將其更改爲true。我原來的問題是關於服務器端未能將graph_sub設置爲true。 – Jules 2014-06-05 13:40:03

+0

是的,這可能有點OT,但我確實認爲'ready'最終應該在一秒鐘後改變(如果它達到那麼多,那麼會再次改變十億次),直到那時'setTimeout'已經通過'while'循環可怕的次數。 – 2014-06-08 13:09:29

回答

0
while !(Session.get 'graph_sub')  
    Meteor.setTimeout (Session.get 'graph_sub'), 1000 

不應該第二行是Session.set?否則會話值永遠不會改變,while循環永遠不會結束。

除了錯字我很困惑,爲什麼你首先使用setTimeoutgraph_sub。這不夠嗎?

if Meteor.isClient 
    Meteor.startup -> 
    Meteor.autorun -> 
     Meteor.subscribe 'graphdata', Session.get 'graph_name' 

if Meteor.isServer 
    Meteor.startup -> 
    Meteor.publish 'graphdata', (name) -> 
     GraphData.find name: name 
+0

是的,對不起,類型。我使用graph_sub作爲檢查訂閱成功的一種方式,因爲Collection可能相當大,需要一段時間才能訂閱。我最初準確地知道你有什麼,但沒有延遲,我的過程在訂閱完成之前結束購買。 – Jules 2013-03-20 21:50:53

0

我想你應該在Meteor.subscribe回調Do something with the GraphData subscription

Meteor.startup -> 
    Meteor.autorun() -> 
    Meteor.subscribe 'graphdata', Session.get 'graph_name',() -> 
     Do something with the GraphData subscription 

還要注意,會話不可用在服務器端了,因爲0.5.8:https://github.com/meteor/meteor/blob/master/History.md#v058

+0

GraphData集合中有三組數據:作爲graph_name傳入的應用程序,設備和網關。使用GraphData訂閱執行某些操作與每個不同的數據集不同。我無法將它移動到Meteor.subscribe下。 – Jules 2013-03-20 21:54:50