2015-06-09 90 views
0

我有一個定義發佈/訂閱行爲的文件,但它只能運行10次。當它不起作用時,查詢返回爲未定義。我可以在chrome的控制檯中執行成功的查詢,所以在查詢觸發後它必須加載。流星發佈/訂閱競賽條件

[項目根] /lib/site.js

Articles = new Mongo.Collection("articles"); 
Authors = new Mongo.Collection("authors"); 

if (Meteor.isServer) { 
    Meteor.publish("articles", function() { 
     return Articles.find(); 
    }); 
    Meteor.publish("authors", function() { 
     return Authors.find(); 
    }); 
} 

if (Meteor.isClient) { 
    Meteor.subscribe("articles"); 
    Meteor.subscribe("authors"); 
} 

[項目根] /client/modules/articleAdHelper.js

var getArticle = function (id) { 
    var article = Articles.findOne({articleID: id}); 
    var author = Authors.findOne({_id: article.authorID}); 
    article.authorName = author.authorName; 
    article.authorPageUrl = author.authorPageUrl; 
    article.authorAvatar = author.authorAvatar; 
    article.articlepublishTimePassedMessage = getPublishDateMessage(article.articlePublishDatetime); 
    return article; 
}; 

var getAdUnit = function (unitKey) { 
    for (var i = 0; i < unitKey.length; i++) { 
     unitKey[i] = getArticle(unitKey[i]); 
    } 
    return { 
     m: [ 
      unitKey[0], 
      unitKey[1] 
     ], 
     h: [ 
      unitKey[2], 
      unitKey[3], 
      unitKey[4], 
      unitKey[5] 
     ] 
    } 
}; 


var articleID = "testarticle"; 
var adUnitObj = getAdUnit([ 
    "testarticle", 
    "claudeArticle", 
    "emusk", 
    "claudeArticle", 
    "testarticle", 
    "adCreatePageTest" 
]); 
Template.frontPageArticleAdUnit.helpers(adUnitObj); 

回答

0

最根本的問題是,你只叫getAdUnit一次,只有在通話時訂閱已準備就緒的情況下(如您找到的那樣)纔有效。我如果你想有一個不反應的解決方案還不清楚,所以我會提出了兩種選擇:

非活性

如果你想進行一次(這樣的廣告產生的廣告單元數據不要更改),那麼在呈現頁面之前,您需要等待訂閱。如果您使用鐵路路由器,則需要在路由中添加waitOn掛鉤。

反應

如果廣告單元的數據可以改變,你需要返回功能爲你的助手,而不是從單一的通話產生的一個對象。這樣數據將隨作者和文章變得可供客戶更新而更新。

我建議您閱讀this post關於在流星中使用衛兵。