首页 文章

MeteorJS:模板渲染和Meteor方法调用

提问于
浏览
0

这个问题跟我前一个问题有关:Meteor: How to publish custom JSON data

我创建了一个构建复杂JSON(使用不同集合等构建)的Meteor方法 . 有了这些数据,我需要使用D3.js构建一个图表 .

我曾经在Session中设置数据,但是当我意识到每次到达页面时都需要刷新它,它无法正常工作 .

显然我在机制中有一些我不理解的东西,但这是我的代码:

Template.myPage.rendered = function(){

    var data = Meteor.call('myData');

    var svgContainer = d3.select("#svg");

    var margin = 40;
    var width = parseInt(d3.select("#svg").style('width'),10);
    var height = parseInt(d3.select("#svg").style('height'),10);
    // ... some more D3 code where I need the data

我得到的错误是:

"Exception from Tracker afterFlush function: data is undefined

I have already tried to call the Meteor Method outside the Template and put the result in the Session (in the callback function), but it did not work. I've also tried to use reactive var, but I couldn't make it.

非常感谢 .

4 回答

  • 1

    好吧,当您尝试使用没有值的变量时,会出现 "undefined" 错误 .

    console.log(data) //return undefined
    

    根据你的说法,Meteor.method返回一个依赖于其他集合的 JSON .

    所以我认为你在这里的最佳选择是在 myPage 模板上使用 waitOn .

    waitOn:function(){
     //return all the collection that the JSON depends
    }
    

    您可以尝试使用会话,也可以使用跟踪器 .

    var data = Meteor.call('myData');
    Session.set('data',data)
    
    //and later
    
         Template.myPage.rendered = functiopn(){
           Tracker.autorun(function(){
             var data = Session.get('data')
              if(data === undefined){
               //this is the long way
               console.log("Wait the data value still undefined")
              }else{
               //load all the D3 stuff
               console.log(data)
              }
           });
         }
    

    GL

  • 1

    我会尝试这样的smth:

    Template.myPage.rendered = function(){
        Meteor.call('myData', function(error,data){
           // if !error, data should be good
           var svgContainer = d3.select("#svg");
    
           var margin = 40;
           var width = parseInt(d3.select("#svg").style('width'),10);
           var height = parseInt(d3.select("#svg").style('height'),10);
        });
    };
    
  • 1

    Thanks to twitter and @callmephilip 我找到了答案(我很惭愧,我自己找不到!:D)

    唯一要做的就是 put all the d3 code inside the method callback

    Template.myPage.rendered = function(){
        Meteor.call('myData', function(err, result){
            if(err){
               console.log(err);
            }else{
                 // all the D3.js code 
            }
        });
    };
    

    祝你有美好的一天 ;)

  • 0

    你可以使用"meteor-reactive-method" . 这是https://github.com/stubailo/meteor-reactive-method的内联链接

    然后在客户端

    Template.myPage.rendered = functiopn(){
       Tracker.autorun(function(){
         var data = ReactiveMethod.call('myData');
          if(data === undefined){
           //this is the long way
           console.log("Wait the data value still undefined")
          }else{
           //load all the D3 stuff
           console.log(data)
          }
       });
     }
    

    在服务器中

    Meteor.methods({
      myData: function() {
        return CollectionName.find().fetch()
     }
    })
    

相关问题