You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/lesson4/tutorial.md
+5-201Lines changed: 5 additions & 201 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -228,213 +228,17 @@ Well done, you've finished! For a bonus, switch your `getGithubInfo` method to r
228
228
> Coach... explain the difference between synchronous and asynchronous requests. There's a good explanation on [Mozilla Developer Network (MDN)](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests)
**Part 2 of this exercise is no longer possible as the API that it uses has been taken down by the BBC.**
236
-
**Sorry, we're working on fixing the exercise!**
235
+
In the following Vonage API tutorial you will create an account, and send yourself an SMS. Just a heads up, it will involve you creating an account and giving your phone number.
237
236
238
-
[Download](https://gist.github.com/despo/05cab2f0b38bc02318e7/download)the exercise files or clone them directly from github `git clone https://gist.github.com/05cab2f0b38bc02318e7.git`
237
+
When signing up select NodeJS as you're language. NodeJS is a runtine enviroment that allows you to run JavaScript on the server than in the browser which is what you would've been doing if you've followed our first 2 tutorials.
239
238
240
-
For the second exercise, we will build an application that retrieves tomorrow's TV schedule for each genre using BBC's API.
239
+
(Send yourself an SMS with Vonage)[https://developer.nexmo.com/?utm_source=codebar&utm_campaign=Events&utm_medium=bitly&utm_source=Events]
241
240
242
-
### What we will be doing:
243
241
244
-
1. Retrieve and render available genres using `http://www.bbc.co.uk/tv/programmes/genres.json`
245
-
246
-
2. Write a function that retrieves tomorrow's TV schedule using a genre `http://www.bbc.co.uk/tv/programmes/genres/<genre>/schedules/tomorrow.json`
247
-
248
-
3. Write a function that displays each programme
249
-
250
-
4.**Bonus** Retrieve all upcoming episodes of a programme
251
-
252
-
### Request using jQuery
253
-
254
-
This time, let's use jQuery's `ajax()` method. Things are a bit easier when using jQuery as we can create different code blocks that handle successful or failed requests.
255
-
256
-
Also, jQuery isolates us from the differences between browser implementations of AJAX calls (for example, if we wanted to make the previous AJAX call work in Internet Explorer, we will have to write [a much longer method](http://www.tutorialspoint.com/ajax/ajax_browser_support.htm)!)
257
-
258
-
```js
259
-
$.ajax({
260
-
url: request_url,
261
-
dataType:'json',
262
-
beforeSend:function() {
263
-
// do something before running the request
264
-
}
265
-
}).done(function(data) {
266
-
// process data
267
-
}).fail(function() {
268
-
// code
269
-
}).always(function() {
270
-
// code that runs regardless of request succeeding or failing
271
-
});
272
-
```
273
-
274
-
`datatype` defines the type of result we will be getting back. This avoids us having to parse the response to JSON.
275
-
276
-
`beforeSend` can be used if we need to perform something before running the request.
277
-
278
-
`.done()` handles a response that returns a success status code
279
-
280
-
`.fail()` is called when the request fails
281
-
282
-
283
-
## Retrieving and displaying all available genres
284
-
285
-
Write a function `retrieveGenres()` that does an AJAX call to the API.
286
-
287
-
```javascript
288
-
functionretrieveGenres() {
289
-
// AJAX call using jQuery that retrieve and process the result
290
-
}
291
-
```
292
-
> Try logging the resulted data and have a look in the console to see and explore the created objects
293
-
294
-
> `<key>` is the genre format we need to retrieve results from the API
295
-
296
-
> You can use `<title>` to display a humanly readable format of the Genre
297
-
298
-
As you can see from the console, the resulting objects are returned inside an Array. We want to iterate over the list using the native Array `forEach( )` function and add each item to the `#genres` list, as a **list item**. As we need to have access to the `key` as well, we can set that as the list item's `id`.
299
-
300
-
Now that we have all the available genres, we can move on to making calls to the API using the genre to retrieve tomorrow's schedule!!
301
-
302
-
## Retrieve schedule
303
-
304
-
Now, let's create a function that retrieves films using genre.
305
-
306
-
```javascript
307
-
functiongetTomorrowsSchedule(genre) {
308
-
// call to retrieve TV schedule
309
-
}
310
-
```
311
-
312
-
The response you get back should look similar to this, with multiple objects in the broadcasts array.
313
-
314
-
```json
315
-
{
316
-
"broadcasts": [
317
-
{
318
-
"is_repeat": false,
319
-
"is_blanked": false,
320
-
"schedule_date": "2014-01-15",
321
-
"start": "2014-01-15T00:10:00Z",
322
-
"end": "2014-01-15T01:50:00Z",
323
-
"duration": 6000,
324
-
"service": {
325
-
"type": "tv",
326
-
"id": "bbc_one",
327
-
"key": "bbcone",
328
-
"title": "BBC One",
329
-
"outlets": [
330
-
{
331
-
"id": "bbc_one_wales",
332
-
"key": "wales",
333
-
"title": "Wales"
334
-
},
335
-
{
336
-
"id": "bbc_one_wales_hd",
337
-
"key": "wales_hd",
338
-
"title": "Wales HD"
339
-
}
340
-
]
341
-
},
342
-
"programme": {
343
-
"type": "episode",
344
-
"pid": "b00sbk03",
345
-
"position": null,
346
-
"title": "Disturbia",
347
-
"short_synopsis": "Thriller about a high school student convinced that his neighbour is a serial killer.",
// display duration (HINT: the duration is in seconds, convert that to minutes)
395
-
// display the channel (or service, as its called by the API) - add this in a span with the class `service`
396
-
...
397
-
}
398
-
```
399
-
400
-
> To display the date formatted correctly, you can use the `formatDate( )` function as we won't be going into details about dates in this tutorial. If you want to know how it works, try going through the code and ask your coach any questions you have.
401
-
402
-
To display an image for a programme, we need to use `<img src=http://ichef.bbci.co.uk/images/ic/272x153/<pid>.jpg />`. As not all programmes have an image, we can use an image placeholder when no image is set `<img src='http://placehold.it/272x153' />`
403
-
404
-
### Binding the call to the click event
405
-
406
-
Handle a `click` event on `#genres li` and make a call to `getTomorrowsSchedule(genre)`
407
-
408
-
### Improving our function
409
-
410
-
To make the genre we have just clicked active, we also want to add the CSS class `active` to the element that the event has been triggered from. Don't forget to remove the class `active` from any other `#genres li` items.
411
-
412
-
> Did you remember to commit your changes?
413
-
414
-
### Using `beforeSend`
415
-
416
-
Every time we issue a call to the API, we want to clear the `#programmes` list. We can do that using `empty()`.
417
-
Also, as some of the requests take a while, we want to display a spinning image `<div class='spinner'><img src='spinner.gif' /></div>`.
418
-
419
-
> Don't forget to remove the spinner, when the request is completed successfully.
420
-
421
-
## Bonus: Retrieving all upcoming episodes of a programme
422
-
423
-
To get back all the upcoming shows for an episode, we need to utilise the programme pid, that we can retrieve from the response using `episode.programme.programme.pid`. The URL for the request is `http://www.bbc.co.uk/programmes/<pid>/episodes/upcoming.json`
424
-
425
-
> **Hint:** The programme `pid` is only available if `episode.programme.position` is set.
426
-
427
-
428
-
```javascript
429
-
functiongetUpcomingEpisodes(pid) {
430
-
// AJAX call to retrieve upcoming episodes
431
-
}
432
-
```
433
-
Since the response structure is similar to the one for retrieving tomorrow's schedule, we should be able to re-use the `processEpisode( )` function to display each item from the broadcasts array.
434
-
435
-
Handle the `click` event to retrieve and display the upcoming episodes!
436
-
437
-
Here is our version of the [tv schedule app](../../examples/tv-schedule/index.html).
438
242
439
243
---
440
244
This ends our **HTTP Requests, AJAX and APIs** tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial [send us an email](mailto:feedback@codebar.io) and let us know.
0 commit comments