forked from michealwillard/CS290-HowTo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection4.html
More file actions
227 lines (212 loc) · 10.4 KB
/
section4.html
File metadata and controls
227 lines (212 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
<head>
<!--
Micheal Willard
CS 290-400
Winter 2015
-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>MLBAM API How-To</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/blog.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="index.html">Home</a>
</nav>
</div>
</div>
<div class="container">
<div class="blog-header">
<!-- <h1 class="blog-title">Introduction to Accessing the MLBAM API</h1>
<p class="lead blog-description">by Micheal Willard</p> -->
</div>
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">Section 4: Accessing the Scoreboard Data Using Javascript</h2>
<hr>
<h3>Making the AJAX Call</h3>
<p>
I'm now going to walk through the code that will generate a very simplistic scoreboard for a <b>selected day</b> that is built off of the <b>minscoreboard.json</b> data. Let's assume we are going to access the Day Level Folder using Javascript and an AJAX call (the method for PHP is very similar). The first thing we will need is a function for making the AJAX call to the API. The <code>getScores()</code> function is triggered by an HTML submit button.
</p>
<pre><code>function getScores() {
var emptyNode = document.getElementById('scoreboard');
emptyNode.innerHTML = '';
var req = new XMLHttpRequest();
if(!req) {
throw 'Unable to create HttpRequest.';
}
var yearSelect = document.getElementsByName('year')[0].value;
var monthSelect = document.getElementsByName('month')[0].value;
var daySelect = document.getElementsByName('day')[0].value;
var url = 'http://gd2.mlb.com/components/game/mlb/';
url += 'year_' + yearSelect + '/month_' + monthSelect + '/day_' + daySelect + '/miniscoreboard.json';
req.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status === 200) {
var response = JSON.parse(this.responseText);
createScoreboard(document.getElementById('scoreboard'), response);
}
}
};
req.open('GET', url);
req.send();
}
</code></pre>
<p>
The <code>yearSelect</code>, <code>monthSelect</code>, <code>daySelect</code> can be variables input by the user through an HTML form or another method. We can see the base URL at <code>url = 'http://gd2.mlb.com/components/game/mlb/';</code>. The URL the concatenates the year, month, day and <b>miniscoreboard.json</b> filename.</p>
<p> Next, we parse the JSON we have received with: <pre><code>var response = JSON.parse(this.responseText);</code></pre> Finally, a function is called to handle the return value: <pre><code>createScoreboard(document.getElementById('scoreboard'), response);</code></pre>
</p>
<h3>Making a Game Object</h3>
<p>
Taking an Object Oriented approach, we can simplify the process of outputting the scoreboard data. So let's create and Object called <b>Game</b> which will represent each game that day.
</p>
<pre><code>function Game() {
this.homeTeam = '';
this.awayTeam = '';
this.homeScore = '';
this.awayScore = '';
// Setters
this.setHomeTeam = function(homeTeam) {
this.homeTeam = homeTeam;
};
this.setAwayTeam = function(awayTeam) {
this.awayTeam = awayTeam;
};
this.setHomeScore = function(homeScore) {
this.homeScore = homeScore;
};
this.setAwayScore = function(awayScore) {
this.awayScore = awayScore;
};
// Getters
this.getHomeTeam = function(homeTeam) {
return this.homeTeam;
};
this.getAwayTeam = function(awayTeam) {
return this.awayTeam;
};
this.getHomeScore = function(homeScore) {
return this.homeScore;
};
this.getAwayScore = function(awayScore) {
return this.awayScore;
};
}</code></pre>
<h3>Populate the Object</h3>
<p>
The Game Objects will need to be populated. We have called the <code>createScoreboard()</code> function within the <code>getScores()</code> function. This function takes the 2 parameters:
<ul>
<li><code>scoreTable</code> is an HTML element where the scoreboard will be displayed.</li>
<li><code>response</code> is the parse JSON responseText we received.</li>
</ul>
We set the <b>obj</b> variable to step down the tiers of attributes we saw in Section 3. Then all that is needed is to call a for-in loop to grab each attribute we are looking for and set the value using the <b>Setter</b> functions in the Game Object. FInally, <code>tableObject()</code> is called to place the new Game object into an HTML Table Element.
</p>
<pre><code>function createScoreboard(scoreTable, response) {
var obj = response.data.games.game;
for (var key in obj) {
var tempItem = new Game();
tempItem.setHomeTeam(obj[key].home_team_name);
tempItem.setAwayTeam(obj[key].away_team_name);
tempItem.setHomeScore(obj[key].home_team_runs);
tempItem.setAwayScore(obj[key].away_team_runs);
tableObject(scoreTable, tempItem);
}
}</code></pre>
<h3>Building Table Elements</h3>
<p>
I won't go into too much detail on the <code>tableObject()</code> as it is pretty specific to this application. What it does though is utilize the <b>Getter</b> functions to create HTML Table Elements and populate them with the Game Object data.
</p>
<pre><code>function tableObject(scoreTable, tempItem) {
var newTable = document.createElement('table');
var tblBody = document.createElement('tbody');
// Build the Away Team Name and Score
var row1 = document.createElement('tr');
var th1 = document.createElement('th');
var th1Text = document.createTextNode(tempItem.getAwayTeam());
th1.appendChild(th1Text);
row1.appendChild(th1);
var td1 = document.createElement('td');
var td1Text = document.createTextNode(tempItem.getAwayScore());
td1.appendChild(td1Text);
row1.appendChild(td1);
tblBody.appendChild(row1);
// Build the Home Team Name and Score
var row2 = document.createElement('tr');
var th2 = document.createElement('th');
var th2Text = document.createTextNode(tempItem.getHomeTeam());
th2.appendChild(th2Text);
row2.appendChild(th2);
var td2 = document.createElement('td');
var td2Text = document.createTextNode(tempItem.getHomeScore());
td2.appendChild(td2Text);
row2.appendChild(td2);
tblBody.appendChild(row2);
newTable.appendChild(tblBody);
scoreTable.appendChild(newTable);
newTable.setAttribute("class", "table table-bordered");
}</code></pre>
<h2>Click "Next" to Demo the Code!</h2>
<!-- <blockquote>
<p>Curabitur blandit tempus porttitor. <strong>Nullam quis risus eget urna mollis</strong> ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</blockquote> -->
</div><!-- /.blog-post -->
<nav>
<ul class="pager">
<li><a href="section3.html">Previous</a></li>
<li><a href="example.html">Next</a></li>
</ul>
</nav>
</div><!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-inset">
<h4>About</h4>
<p>This site offers a How-To on accessing the MLB Advanced Media Api. The code is all done in Javascript, but PHP has similar methods which can be applied to attain the same effect if server side scripting is desired.</p>
</div>
<div class="sidebar-module">
<h4>Sections</h4>
<ol class="list-unstyled">
<li><a href="section1.html">Section 1: The Structure of the API Data</a></li>
<li><a href="section2.html">Section 2: The Date Level Folder</a></li>
<li><a href="section3.html">Section 3: The Scoreboard JSONs</a></li>
<li><a href="example.html">Section 5: A Scoreboard Example Using Javascript</a></li>
</ol>
</div>
<div class="sidebar-module">
<h4>Elsewhere</h4>
<ol class="list-unstyled">
<li><a href="https://github.com/michealwillard">GitHub</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div>
<footer class="blog-footer">
<p>Blog template built for <a href="http://getbootstrap.com">Bootstrap</a> by <a href="https://twitter.com/mdo">@mdo</a>.</p>
<p>
<a href="#">Back to top</a>
</p>
</footer>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>