Meteor - return asynchronous function to handlebar template? -


i trying generate flickr url based on flickr api call, , return result handlebars.js template. struggling find way around asynchronous processes.

i have tried create callback function, still uncertain how defined object or variable html template.

here code 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 this:

template.backgroundimage.background = function(){     flickrrandomphotofromset(setid,function(){         return flickrobject;     }); }; 

but still leaves me stuck, not able defined object html, coded such:

<template name="backgroundimage"> <div id="background">     {{random}} </div> 

use session intermediary. reactive set change template new data:

template.backgroundimage.background = function(){     return session.get("flickrobject"); };  template.backgroundimage.created = function() {     flickrrandomphotofromset(setid,function(){         session.set("flickrobject", flickrobject)     }); } 

so created method run when template created run flickrrandomphotofromset, when result returned set session hash in turn set background result received.

be careful flickrrandomphotofromset too, didn't notice had argument flickrobject pass callback.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -