0

I have a Meteor method that retrieves Google Books data

searchBooks: function (query) {
  this.unblock();
  return Meteor.http.call("GET", "https://www.googleapis.com/books/v1/volumes?q=" + query);
}

which is called like this

Meteor.call("searchBooks", term, function(error, results) {
    console.log(results.content);
});

How do I go about making the results renderable in a template (with handlebars)?

cavill
  • 590
  • 6
  • 19
  • 2
    Solution [here](http://stackoverflow.com/a/22148624/728291) should work. – user728291 Jun 25 '14 at 13:23
  • possible duplicate of [How to use meteor methods inside of a template helper](http://stackoverflow.com/questions/22147813/how-to-use-meteor-methods-inside-of-a-template-helper) – Hubert OG Jun 25 '14 at 14:13
  • I don't think my question is answered in the suggested dupe as both answers refer to rendering on `Template.template.created`, whereas this is occurring within an already rendered template. Though I'd be very happy to be told I'm wrong here. – cavill Jun 25 '14 at 15:23
  • Put it into a variable. Return that variable from a template helper. Blaze knows how to iterate `#each` over an array for example. – imslavko Jun 25 '14 at 16:51
  • I don't think it is particularly important when your data gets updated (ie when your request returns: before or after template is rendered). – imslavko Jun 25 '14 at 16:55

1 Answers1

1

Put your results into a reactive variable, for example in Session:

Meteor.call("searchBooks", term, function(error, results) {
    Session.set('books', results.content);
});

Return this variable in a template helper:

Template.booksView.helpers({
  books: function() {return Session.get('books');}
});

Then, in the template use double braces for usual text and in triple braces for html:

<Template name='booksView'>
  {{{books}}}
</Template>

You might have to parse or iterate your data depending on its format and output requirements.

Evgeny Lukianchikov
  • 3,851
  • 3
  • 15
  • 9