Denis
Denis

Reputation: 7343

How to render a static page with predefined data in Meteor

I want to render simple static page with a data which is get from a remote api. For example I want to render a page with weather cast which I get from an external service. But it's doesn't work.

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

So, on a page is displayed nothing. How to pass a data to template without any reactive contexts like a mongo collections or sessions?

Upvotes: 1

Views: 314

Answers (1)

Tarang
Tarang

Reputation: 75955

Relay the data with 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);};
    });
}

You need to be careful with callbacks in javascript, when you use a callback the return statement isn't passed to the original function because the callback makes it async

Upvotes: 6

Related Questions