2015-06-09 94 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关于在流星中使用卫兵。