【发布时间】:2014-01-04 06:12:18
【问题描述】:
我正在学习如何使用 Meteor,我正在尝试连接到 google map api 并返回 json 使用meteor.http.get。下面的代码工作正常,我可以设置模板变量test等于返回的json并查看它(我现在想用它来学习):
if (Meteor.isServer) {
Meteor.methods({
getGoogleMaps: function () {
this.unblock();
return Meteor.http.call("GET", "http://maps.googleapis.com/maps/api/geocode/json",
{params:{address:"8-10 Broadway, London SW1H 0BG,United Kingdom",
sensor:false}});
}
});
}
if (Meteor.isClient) {
Template.main.test=function(){ return Session.get("response");}
Meteor.call("getGoogleMaps", function(error, results) {
Session.set("response", results.content);
});
}
但是以下将返回的json分配给测试模板变量的方法不起作用:
if (Meteor.isClient) {
var response;
Meteor.call("getResponses", function(error, results) {
response= results.content;
});
Template.main.test=function(){ return response;}
}
这也不起作用:
if (Meteor.isClient) {
Meteor.call("getResponses", function(error, results) {
Template.main.test= results.content;
});
}
为什么最后两种方法不起作用?从rest api返回的结果中设置模板变量的最合适方法是什么?
【问题讨论】:
-
这个回答你的问题,我认为第一种方式是合适的方式stackoverflow.com/questions/16533637/…
标签: javascript rest meteor