Professional Documents
Culture Documents
METEORCAPTURE
Follow@dburles
HOME
SUBSCRIBE
Publishing data
from an external
API
16 SEPTEMBER 2014
1/10
3/26/2015
side collection.
In the case where the API requires private
authentication or perhaps we need to decorate the
document with some other server side data, the
publication method demonstrated here is the
approach to take.
We're using a public API here simply for
demonstration.
Client
Let's start off once again with the client side code
Templates
<body>
<form>
<input type="text" name="query">
<input type="submit" value="Search">
</form>
{{#if searching}}
<p>Searching...</p>
{{else}}
http://meteorcapture.com/publishing-data-from-an-external-api/
2/10
3/26/2015
{{#if books.count}}
<hr>
<table>
{{#each books}}
{{> book}}
{{/each}}
</table>
{{/if}}
{{/if}}
</body>
<template name="book">
<tr>
<td class="image"><img src="{{thumb}}"></td>
<td>
<a href="{{link}}" target="_blank">{{title}}</a>
<p>{{{snippet}}}</p>
</td>
</tr>
</template>
Javascript
Books = new Mongo.Collection('books');
http://meteorcapture.com/publishing-data-from-an-external-api/
3/10
3/26/2015
Session.setDefault('searching', false);
Tracker.autorun(function() {
if (Session.get('query')) {
var searchHandle = Meteor.subscribe('booksSearch',
Session.get('query'));
Session.set('searching', ! searchHandle.ready());
}
});
Template.body.events({
'submit form': function(event, template) {
event.preventDefault();
var query = template.$('input[type=text]').val();
if (query)
Session.set('query', query);
}
});
Template.body.helpers({
books: function() {
return Books.find();
},
searching: function() {
return Session.get('searching');
http://meteorcapture.com/publishing-data-from-an-external-api/
4/10
3/26/2015
}
});
Server
Now let's take a look at what's going on over on the
server. This is where the interesting stuff happens.
Meteor.publish('booksSearch', function(query) {
var self = this;
try {
var response =
HTTP.get('https://www.googleapis.com/books/v1/volumes', {
params: {
http://meteorcapture.com/publishing-data-from-an-external-api/
5/10
3/26/2015
q: query
}
});
_.each(response.data.items, function(item) {
var doc = {
thumb: item.volumeInfo.imageLinks.smallThumbnail,
title: item.volumeInfo.title,
link: item.volumeInfo.infoLink,
snippet: item.searchInfo &&
item.searchInfo.textSnippet
};
self.ready();
} catch(error) {
console.log(error);
}
});
6/10
3/26/2015
Example app
https://github.com/dburles/meteor-capturepubsub2
Check out the example application on GitHub.
http://meteorpad.com/pad/YWbRFASEfC3NQgfN8
http://meteorcapture.com/publishing-data-from-an-external-api/
7/10
3/26/2015
David Burles
Meteor Melbourne.
Melbourne, Australia
13Comments
GitHub
MeteorCapture
Recommend 2
Share
Login
SortbyBest
Jointhediscussion
bsbechtel 3monthsago
Thisisgreat.Thanksforwritingthis!
1
Reply Share
MorganJackson 6monthsago
GreatArticleDave!
1
Reply Share
DavidBurles
Mod >MorganJackson
6monthsago
ThanksMorgan!
Reply Share
StuartMitchell 6monthsago
NicearticleDave!Easytofollowandnottoolong.Imightevenrollupmy
sleevesandgiveitagowiththediscogsAPIsoIcanfinallycatalogueallmy
oldvinyl:D
1
Reply Share
LucianoGuasco 6monthsago
VerynicearticleDavid.
FeelingtemptedtorefactormanyMeteor.methodscallingAPIstosomething
similarlikethis.
Definitelywiththisapproach,thepyramidofcallbacksisreducedoratleast
http://meteorcapture.com/publishing-data-from-an-external-api/
8/10
3/26/2015
Definitelywiththisapproach,thepyramidofcallbacksisreducedoratleast
removedfromtemplateslogic.
1
Reply Share
CasperSrensen 12daysago
HeyDavid,
Greattutorial!Iwentaheadandactuallygotitimplementedwith
themoviedb.orgtofetchalistoftvshowsinsteadofthebooks.Hopetosee
somemoreofthisstuffinthefuture,welldone!:)
Reply Share
DavidBurles
Mod >CasperSrensen
11daysago
That'sgreat,thanks!
Reply Share
WidaSkyDev amonthago
Nicearticledave!!thank's
Reply Share
greg amonthago
HowwouldIuseatwitterstreamapiinthesamefashion.Tosearchfortweets.
Reply Share
DavidBurles
Mod >greg
amonthago
Goodquestion,unfortunatelyI'mnotfamiliarenoughwiththetwitter
APItobeabletoanswerthatonesorry!
Reply Share
KarlPokus 6monthsago
Toobadmeteorpadisreeallyslow.
Reply Share
DavidBurles
Mod >KarlPokus
6monthsago
YeahIknow!,it'sgreatwhenit'sworkingthough:)
Reply Share
KarlPokus>DavidBurles 6monthsago
Absolutely!Acodepen.ioforMeteorisawesomebutthelagis
abitannoying.Hopefullyitwillgetbetter.
1
Reply Share
WHAT'STHIS?
ALSOONMETEORCAPTURE
Understandingspacebars
Random
16comments9monthsago
3comments7monthsago
imslavkoIwouldaddthat
http://meteorcapture.com/publishing-data-from-an-external-api/
LucianoGuascoDavid,you
9/10
3/26/2015
imslavkoIwouldaddthat
observingacursorismorepreferable
toreturningafetchedarray:Blazehas
anoptimizationforcursorswhenit
LucianoGuascoDavid,you
alwaysappearwhenIneedyou.
Happynewyearmate:)
Publishinganything
Joiningontheclient
11comments6monthsago
3comments9monthsago
AvatarDavidBurlesHeyJosh,itwasone
AvatarDominiquePerettiHey,IthinkI
http://meteorcapture.com/publishing-data-from-an-external-api/
10/10