Get Programming with JavaScript - Listings 19.11 and 19.12

Listings 19.11 and 19.12 - A modular news page

HTML:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Fitness App News</title> </head> <body> <h1>Fitness App News</h1> <div id="news"></news> <script type="text/template" id="newsItemTemplate"> <div class="newsItem"> <h3>{{title}}<span> - by {{author}}</span></h3> <p>{{body}}</p> <p class="posted"><em>Posted: {{posted}}</em></p> <p class="follow">Follow {{author}} {{social}}</p> </div> </script> <!-- templates module --> <script src="http://output.jsbin.com/pugase.js"></script> <!-- news items data --> <script src="http://output.jsbin.com/fupiki.js"></script> </body> </html>

JS:

(function () { "use strict"; var templateScript = document.getElementById("newsItemTemplate"); var templateString = templateScript.innerHTML; var newsContainer = document.getElementById("news"); var newsData = fitnessApp.news.getItems(3); newsContainer.innerHTML = gpwj.templates.fillList(templateString, newsData); })();

Further Adventures

Listings 19.11 and 19.12 - A modular news page - Tasks 1 to 4

HTML:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Fitness App News</title> </head> <body> <h1>Fitness App News</h1> <p> <select id="ddItems"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> <button id="btnShow">Show</button> </p> <div id="news"></news> <script type="text/template" id="newsItemTemplate"> <div class="newsItem"> <h3>{{title}}<span> - by {{author}}</span></h3> <p>{{body}}</p> <p class="posted"><em>Posted: {{posted}}</em></p> <p class="follow">Follow {{author}} {{social}}</p> </div> </script> <!-- templates module --> <script src="http://output.jsbin.com/pugase.js"></script> <!-- news items data --> <script src="http://output.jsbin.com/fupiki.js"></script> </body> </html>

JS:

(function () { "use strict"; var templateScript = document.getElementById("newsItemTemplate"); var templateString = templateScript.innerHTML; var newsContainer = document.getElementById("news"); var ddItems = document.getElementById("ddItems"); var btn = document.getElementById("btnShow"); function showItems (numItems) { var newsData = fitnessApp.news.getItems(numItems); newsContainer.innerHTML = gpwj.templates.fillList(templateString, newsData); } function update () { var numItems = ddItems.value; showItems(numItems); } btn.addEventListener('click', update); })();