Client-Side Templating & Fetch
CS 396: �Introduction to Web Development
Spring, 2021
Announcements
Outline
Outline
Recall: Server-Side Templates
In HW2, the Photo App interface was generated on the server. Specifically:
Next Two Weeks: Client-Side Templating
In HW4, we will practice building client-side templates. Specifically:
�Example: https://www.instagram.com/
Outline
Data → HTML
const player = {
name: "Jane",
pic: "http://website.com/avatar.png",
score: 300
};
<div class="card">
<img src="http://website.com/avatar.png">
<p>Jane scored 300 points</p>
</div>
The Data
The HTML�(Goal)
You COULD do one big string concatenation...
...but string concatenation is annoying – difficult to read and easy to make mistakes.
�const html = '<div class="card">' +
'<img src="' + player.pic + '">' +
'<p>' + player.name + ' scored ' +
player.score + ' points</p>' +
'</div>';
9
ES6 Introduced a better way: Client-side templates
` <template goes here> `
${ my_expression }
10
Rewriting previous HTML using template syntax
const html = `
<div class="card">
<img src="${player.pic}">
<p>
${player.name}'s high score is:
${player.score}
</p>
</div>`;
More on templates
const name = 'Walter';
console.log( ` A template
can be multiple lines.
It can also evaluate expressions like:
${2 + 2} or� ${name} or
${getTodaysDate()}
` );
12
Demos
13
Outline
1. Intro to AJAX
AJAX
15
1. Intro to Fetch
JavaScript’s Fetch API
16
1. How Fetch Works
fetch('https://photo-app-demo.herokuapp.com/api/posts')
.then(response => response.json()) // callback function
.then(data => console.log(data)); // callback function
2. What is a Promise?
2. Promises: Two Syntaxes > “then” method
Using the promise’s “then” method, which will execute a callback function when the promise finishes its business...�
let url = 'https://photo-app-demo.herokuapp.com/api/posts'
fetch(url)
.then(response => response.json()) // callback function
.then(data => console.log(data)); // callback function
2. Promises: Two Syntaxes > async await
Using the async / await technique. Instead of using “then()” the await keyword ensures that the next line doesn’t execute until the promise resolves.
const getPosts = async () => {
let url = 'https://photo-app-demo.herokuapp.com/api/posts';
const response = await fetch(url);
console.log(response);
const jsonData = await response.json();
console.log(jsonData);
};
getPosts();
Outline
Error Handling
Example: No Error Handling
fetch('/https://photo-app-demo.herokuapp.com/api/posts')
.then(response => response.json())
.then(data => {
console.log('Success:', data);
});
Example: With Error Handling
fetch('/https://photo-app-demo.herokuapp.com/api/posts')
.then(response => {
if (!response.ok) {
// send to catch block:
throw Error(response.statusText);
} else {
return response.json();
}
})
.then(data => {
console.log('Success:', data);
})
.catch(err => {
console.error('Error:', err);
});
Example: With Error Handling
const getPosts = async () => {
try {
let url = 'https://photo-app-demo.herokuapp.com/api/posts';
const response = await fetch(url);
console.log(response);
const jsonData = await response.json();
console.log(jsonData);
} catch(err) {
console.error(err);
}
};
getPosts();
Outline
Cross-Origin Resource Sharing (CORS)
Practice Time
04-fetch-get
05-fetch-search