Skip to content

Instantly share code, notes, and snippets.

View haingdc's full-sized avatar

Hải haingdc

View GitHub Profile
@haingdc
haingdc / come_together.js
Created April 10, 2018 03:13
Thao tác với DOM thông qua TDD
FlickrFetcher.fetchPhotos("8060d4cdac3ceb86af470aae29af3a56")
.then(PhotoLister.photoListToHTML)
.then(function(photosHTML) {
PhotoLister.addPhotosToElement($, "#mydiv", photosHTML);
});
@haingdc
haingdc / flickr-fetcher01.js
Last active April 10, 2018 03:40
Thao tác với DOM thông qua TDD
//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);
@haingdc
haingdc / photo-lister07.js
Last active April 10, 2018 03:39
Thao tác với DOM thông qua TDD
// photo-lister.js
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = PhotoLister;
}
@haingdc
haingdc / flickr-fetcher.js
Created April 10, 2018 03:10
Thao tác với DOM thông qua TDD
// flickr-fetcher.js
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = FlickrFetcher;
}
@haingdc
haingdc / photo-lister06.js
Last active April 10, 2018 03:38
Thao tác với DOM thông qua TDD
addPhotosToElement: function($, selector, list) {
return $(selector).append(list);
}
@haingdc
haingdc / photo-lister-spec04.js
Last active April 10, 2018 03:26
Thao tác với DOM thông qua TDD
// 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>',
),
@haingdc
haingdc / photo-lister05.js
Last active April 10, 2018 03:38
Thao tác với DOM thông qua TDD
photoListToHTML: function(photos) {
return [
"<ul>",
photos.map(PhotoLister.photoToListItem).join(""),
"</ul>",
].join("");
}
@haingdc
haingdc / photo-lister04.js
Created April 10, 2018 03:05
Thao tác với DOM thông qua TDD
photoListToHTML: function(photos) {
return "<ul>" + photos.map(PhotoLister.photoToListItem).join("") + "</ul>";
}
@haingdc
haingdc / photo-lister-spec03.js
Created April 10, 2018 03:04
Thao tác với DOM thông qua TDD
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",
@haingdc
haingdc / photo-lister03.js
Created April 10, 2018 03:03
Thao tác với DOM thông qua TDD
PhotoLister = {
photoToListItem: function(photo) {
return [
'<li><figure><img src="',
photo.url,
'" alt=""/>',
"<figcaption>",
photo.title,
"</figcaption></figure></li>",
].join("");