Skip to content

Instantly share code, notes, and snippets.

@pdewouters
Created February 22, 2012 13:11
Show Gist options
  • Select an option

  • Save pdewouters/1885106 to your computer and use it in GitHub Desktop.

Select an option

Save pdewouters/1885106 to your computer and use it in GitHub Desktop.
fetch photos on flickr by tag
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Flickr</title>
<style>
body { width: 600px; margin: auto; }
ul { list-style: none; }
li { padding-bottom: 1em; }
img { float: left; padding-right: 1em; }
a { text-decoration: none; color: #333; }
</style>
</head>
<body>
<ul id="flickr-photos">
<script id="photos-template" type="text/x-handlebars-template">
{{#each this}}
<li>
<img src="{{url}}" alt="{{author}}">
</li>
{{/each}}
</script>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js"></script>
<script>
(function() {
var Flickr = {
init: function( config ) {
this.url = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=c14cff17d47f92b0bc0ff19fe4a12625&tags=' + config.query + '&format=json&nojsoncallback=1&api_sig=47ce019381cf16bc172c5dfafc3423c0';
this.template = config.template;
this.container = config.container;
this.fetch();
},
attachTemplate: function() {
var template = Handlebars.compile( this.template );
this.container.append( template( this.photos ) );
},
fetch: function() {
var self = this;
$.getJSON( this.url, function( data ) {
//console.log(data.photos.photo);
self.photos = $.map( data.photos.photo, function( photo ) {
return {
author: photo.owner,
photo: photo.title,
url: 'http://farm' + photo.farm + '.staticflickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_m.jpg'
};
});
// For future lessons, research $.deferred, viewers. :)
self.attachTemplate();
});
}
};
Flickr.init({
template: $('#photos-template').html(),
container: $('#flickr-photos'),
query: 'horses'
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment