Listings 19.11 and 19.12 - A modular news page
JS Bin
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);
})();
Listings 19.11 and 19.12 - A modular news page - Tasks 1 to 4
- Create a showItems function that displays a specified number of news items on the page.
- Add a drop-down list to the page so the reader can choose the number of items to display.
- Create an update function that reads the number of items to display from the drop-down list and then calls showItems with the specified number.
- Add a 'show' button that calls the update function when clicked.
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);
})();