This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FlickrFetcher.fetchPhotos("8060d4cdac3ceb86af470aae29af3a56") | |
.then(PhotoLister.photoListToHTML) | |
.then(function(photosHTML) { | |
PhotoLister.addPhotosToElement($, "#mydiv", photosHTML); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//flickr-fetcher.js | |
fetchFlickrData: function(apiKey, fetch) { | |
if (!fetch && typeof jQuery !== "undefined") { | |
fetch = jQuery.getJSON.bind(jQuery); | |
} | |
var url = | |
"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + | |
apiKey.toString() + | |
"&text=pugs&format=json&nojsoncallback=1"; | |
return fetch(url); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// photo-lister.js | |
if (typeof module !== "undefined" && typeof module.exports !== "undefined") { | |
module.exports = PhotoLister; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// flickr-fetcher.js | |
if (typeof module !== "undefined" && typeof module.exports !== "undefined") { | |
module.exports = FlickrFetcher; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addPhotosToElement: function($, selector, list) { | |
return $(selector).append(list); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// photo-lister-spec.js | |
var cheerio = require("cheerio"); | |
// ... lược bỏ ... | |
describe("#addPhotosToElement()", function() { | |
it("should take an HTML string of list items and add them to an element with a given selector", function() { | |
var $ = cheerio.load( // 🦊 | |
'<html><head></head><body><div id="mydiv"></div></body></html>', | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
photoListToHTML: function(photos) { | |
return [ | |
"<ul>", | |
photos.map(PhotoLister.photoToListItem).join(""), | |
"</ul>", | |
].join(""); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
photoListToHTML: function(photos) { | |
return "<ul>" + photos.map(PhotoLister.photoToListItem).join("") + "</ul>"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("#photoListToHTML()", function() { | |
it("should take an array of photo objects and convert them to an HTML list", function() { | |
var input = [ | |
{ | |
title: "This is a test", | |
url: "http://loremflickr.com/960/593", | |
}, | |
{ | |
title: "This is another test", | |
url: "http://loremflickr.com/960/593/puppy", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PhotoLister = { | |
photoToListItem: function(photo) { | |
return [ | |
'<li><figure><img src="', | |
photo.url, | |
'" alt=""/>', | |
"<figcaption>", | |
photo.title, | |
"</figcaption></figure></li>", | |
].join(""); |
NewerOlder