Meteor - 将异步函数返回到车把模板? [英] Meteor - return asynchronous function to handlebar template?
问题描述
我正在尝试根据 Flickr API 调用生成 Flickr url,然后将该结果返回到 handlebars.js 模板.我正在努力寻找解决异步流程的方法.
我已经尝试创建一个回调函数,但我仍然不确定如何将定义的对象或变量放入 HTML 模板中.
这里是 Flickr API 函数的代码:
var FlickrRandomPhotoFromSet = function(setID,callback){Meteor.http.call("GET","http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key="+apiKey+"&photoset_id="+setID+"&格式=json&nojsoncallback=1",函数(错误,结果){if (result.statusCode === 200)var photoResult = JSON.parse(result.content);var photoCount = photoResult.photoset.total;var randomPhoto = Math.floor((Math.random()*photoCount)+1);var selectedPhoto = photoResult.photoset.photo[randomPhoto];var imageURL = "<img src=http://farm"+selectedPhoto.farm+".staticflickr.com/"+selectedPhoto.server+"/"+selectedPhoto.id+"_"+selectedPhoto.secret+"_b.jpg/>";FlickrObject.random = imageURL;}if (回调&& typeof(callback)==="函数") {打回来();}});};
我的模板代码是这样的:
Template.backgroundImage.background = function(){FlickrRandomPhotoFromSet(setID,function(){返回 FlickrObject;});};
但这仍然让我卡住了,无法将定义的对象放入我的 HTML 中,其编码如下:
<div id="背景">{{随机的}}
使用 Session
作为中介.它是被动的,所以一旦设置它就会用新数据更改模板:
Template.backgroundImage.background = function(){返回 Session.get("FlickrObject");};Template.backgroundImage.created = function() {FlickrRandomPhotoFromSet(setID,function(){Session.set("FlickrObject", FlickrObject)});}
因此 created
方法将在创建模板时运行 FlickrRandomPhotoFromSet
,当返回结果时,它将设置会话哈希值,从而设置会话哈希值收到结果后立即后台.
也要小心你的 FlickrRandomPhotoFromSet
,我没有注意到你有一个参数让 FlickrObject
传递给回调.
I am trying to generate a Flickr url based on a Flickr API call, and then return that result to a handlebars.js template. I am struggling to find a way around asynchronous processes.
I have tried to create a callback function, but I am still uncertain how to get a defined object or variable into the HTML template.
Here is the code for the Flickr API function:
var FlickrRandomPhotoFromSet = function(setID,callback){
Meteor.http.call("GET","http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key="+apiKey+"&photoset_id="+setID+"&format=json&nojsoncallback=1",function (error, result) {
if (result.statusCode === 200)
var photoResult = JSON.parse(result.content);
var photoCount = photoResult.photoset.total;
var randomPhoto = Math.floor((Math.random()*photoCount)+1);
var selectedPhoto = photoResult.photoset.photo[randomPhoto];
var imageURL = "<img src=http://farm"+selectedPhoto.farm+".staticflickr.com/"+selectedPhoto.server+"/"+selectedPhoto.id+"_"+selectedPhoto.secret+"_b.jpg/>";
FlickrObject.random = imageURL;
}
if (callback && typeof(callback)==="function") {
callback();
}
});};
My template code is this:
Template.backgroundImage.background = function(){
FlickrRandomPhotoFromSet(setID,function(){
return FlickrObject;
});
};
But this still leaves me stuck, not able to get a defined object into my HTML, which is coded as such:
<template name="backgroundImage">
<div id="background">
{{random}}
</div>
Use Session
as an intermediary. It is reactive so as soon as its set it will change the template with the new data:
Template.backgroundImage.background = function(){
return Session.get("FlickrObject");
};
Template.backgroundImage.created = function() {
FlickrRandomPhotoFromSet(setID,function(){
Session.set("FlickrObject", FlickrObject)
});
}
So the created
method will be run when the template is created to run FlickrRandomPhotoFromSet
, when the result is returned it will set the Session hash which in turn will set the background as soon as the result is received.
Be careful with your FlickrRandomPhotoFromSet
too, I didn't notice you had an argument for FlickrObject
to pass to the callback.
这篇关于Meteor - 将异步函数返回到车把模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!