Meteor的Template.rendered回调中的数据上下文在热代码推送时随机消失

时间:2014-11-18 15:16:30

标签: meteor meteor-blaze

简短版本:

应该由路由器传递给模板的

Session.get()Template.currentData()通常会在undefined回调中变为Template.<templateName>.render。这种情况非常随机发生,通常是热代码推送,但并非总是如此,而且不仅仅是热代码推送。

更长的版本:

我使用Iron Router将数据上下文传递给模板appBody

Router.route('/:_mapid', function() {
  if (!isNaN(this.params._mapid)) {
    Session.set('currentMap',Maps.findOne({mapid: Number(this.params._mapid)}));
    this.render('appBody', {
        data: function() {  return Session.get("currentMap") }
    });
  }
});

模板然后使用d3生成一堆div,并在Template.appBody.rendered回调中设置动态页面标题:

   Template.appBody.rendered = function() {
    Deps.autorun(function() {
      d3.select("#map_body").selectAll("div").remove();

      d3.select("#map_body").selectAll("div")
      .data(Nodes.find({mapid: Template.currentData().mapid }).fetch(), function(d) {return d.nodeparentid;})
      .enter().append("div")
        .attr("id", function(d){return "node"+ d.nodeparentid})
        .style("position","absolute")
        .style("top",function(d) {return d.toppos+"px"})
        .style("left",function(d) {return d.leftpos+"px"})
        .style("width",function(d) {return d.width+"px"})
        .style("height",function(d) {return d.height+"px"})
        .classed("node", true)

       document.title = "Map - " + Session.get("currentMap").title;

    }

如您所见,我尝试以两种不同的方式将数据上下文传递给模板:通过currentMap设置全局Session.set对象并通过路由器传递data密钥,然后通过Template.currentData()方法访问它。

出于某种原因,这两种方法都经常失败(尽管我仍然无法在什么条件下弄清楚)。以下是我在尝试设置文档标题时从浏览器控制台获得的示例错误:

Exception from Tracker afterFlush function: Cannot read property 'title' of undefined
TypeError: Cannot read property 'title' of undefined
    at http://localhost:3000/client/templates/app_body.js?47b256634607ca16879aa0ed823593aec01ee840:122:31
    at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36)
    at new Tracker.Computation (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:206:10)
    at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:476:11)
    at Template.appBody.rendered (http://localhost:3000/client/templates/app_body.js?47b256634607ca16879aa0ed823593aec01ee840:117:10)
    at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2970:21)
    at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1720:14
    at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2029:12)
    at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1719:15
    at Tracker.flush (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:438:11) 

关于可能导致这种情况的任何想法?

0 个答案:

没有答案