Meteor - return asynchronous function to handlebar template?

前端 未结 1 1333
生来不讨喜
生来不讨喜 2021-01-01 00:12

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 p

相关标签:
1条回答
  • 2021-01-01 00:49

    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.

    0 讨论(0)
提交回复
热议问题