Excercise 7 done: List studios, alert with details
This commit is contained in:
parent
3bd40b6280
commit
56d8f7fca7
@ -7,7 +7,7 @@
|
||||
<script>
|
||||
"use strict";
|
||||
|
||||
const MOVIES_RESOURCE_URL = "http://localhost:8080/movieservice/resources/movie";
|
||||
const STUDIOS_RESOURCE_URL = "http://localhost:8080/movieservice/resources/studio";
|
||||
|
||||
const options = {
|
||||
mode: "cors",
|
||||
@ -17,36 +17,31 @@
|
||||
}
|
||||
};
|
||||
|
||||
let loadNews = function () {
|
||||
// Studios: ID dranhängen -> 2. Aufruf -> Alert dranhängen
|
||||
fetch(MOVIES_RESOURCE_URL, options)
|
||||
let loadStudioList = function () {
|
||||
fetch(STUDIOS_RESOURCE_URL, options)
|
||||
.then(response => response.json())
|
||||
.then(function (moviesList) {
|
||||
let html = moviesList.map(movie =>
|
||||
`<h2>${movie.title}</h2>
|
||||
<p>${movie.description}</p>
|
||||
<button onclick="displayDetails(${movie.id})">Get more info</button>`)
|
||||
.then(function (studioList) {
|
||||
document.getElementById("content").innerHTML = studioList.map(studio =>
|
||||
`<h2>${studio.name} <button onclick="alertStudioDetails(${studio.id})">Get more info</button></h2>`)
|
||||
.join("");
|
||||
|
||||
document.getElementById("content").innerHTML = html;
|
||||
})
|
||||
.catch(error => document.getElementById("content").innerHTML = `Error: ${error}`);
|
||||
};
|
||||
|
||||
let displayDetails = function (id) {
|
||||
fetch(MOVIES_RESOURCE_URL + "/" + id, options) // TODO: Ugly URL building
|
||||
let alertStudioDetails = function (id) {
|
||||
fetch(STUDIOS_RESOURCE_URL + `/${id}`, options) // TODO: Ugly URL building
|
||||
.then(response => response.json())
|
||||
.then(function (movie) {
|
||||
alert(movie.length)
|
||||
.then(function (studio) {
|
||||
alert("Country code: " + studio.countrycode + "\n" + "Post code: " + studio.postcode)
|
||||
})
|
||||
.catch(error => document.getElementById("content").innerHTML = `Error: ${error}`);
|
||||
};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", loadNews);
|
||||
document.addEventListener("DOMContentLoaded", loadStudioList);
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Movies in JavaScript</h1>
|
||||
<h1>Studios</h1>
|
||||
<div id="content"></div>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user