如何在Meteor中呈现具有预定义数据的静态页面

Den*_*nis 1 javascript meteor

我想渲染简单的静态页面,其中包含从远程api获取的数据.例如,我想渲染一个带有天气预报的页面,这是我从外部服务获得的.但它不起作用.

Template.myStaticPage.content = function(){
 Meteor.http('GET', 'http://someurl.com/api/weather', function(err, res){
   if(res){return res};
 })
}
Run Code Online (Sandbox Code Playgroud)

因此,在页面上没有显示任何内容.如何在没有mongo集合或会话等任何被动上下文的情况下将数据传递给模板?

Aks*_*hat 6

使用以下命令中继数据Session:http://docs.meteor.com/#session

Template.myStaticPage.content = function(){
    return Session.get("weather");
}

//Will run when the template is created
Template.myStaticPage.created = function() {
    Meteor.http('GET', 'http://someurl.com/api/weather', function(err, res){
        if(res){Session.set("weather", res);};
    });
}
Run Code Online (Sandbox Code Playgroud)

你需要小心javascript中的回调,当你使用回调时,return语句不会传递给原始function回调,因为回调使它成为异步