[英]How to render a static page with predefined data in Meteor
我想使用從遠程api獲取的數據來呈現簡單的靜態頁面。 例如,我想用從外部服務獲得的天氣預報投射頁面。 但這是行不通的。
Template.myStaticPage.content = function(){
Meteor.http('GET', 'http://someurl.com/api/weather', function(err, res){
if(res){return res};
})
}
因此,頁面上什么也不顯示。 如何在沒有任何反應性上下文(例如mongo集合或會話)的情況下將數據傳遞到模板?
通過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);};
});
}
您需要注意JavaScript中的回調,當您使用回調時,return語句不會傳遞給原始function
因為回調使它異步
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.