๐พ โก๏ธ ๐ป Data to UI
Learning Objectives
When we build user interfaces we often take data and
๐ฝ๏ธ Cinema listings
Learning Objectives
Suppose you’re building a user interface to display the films that are now showing on a film website. We need to render some cinema listings in the user interface. Let’s define an acceptance criterion:
Given a list of film data
When the page first loads
Then it should display the list of films now showing, including the film title, times and film certificate
We can use a
Cinema listings wireframe ๐ผ๏ธ
The wireframe is built by reusing the same
Interactive code block
const films = [
{
title: "Killing of Flower Moon",
director: "Martin Scoresee"
times: ["15:35"],
certificate: "15",
duration: 112
},
{
title: "Typist Artist Pirate King",
directory: "Carol Morley"
times: ["15:00", "20:00"],
certificate: "12A",
duration: 108
},
.
.
.
];
Our task will be to build the film listings view from this list of data.
๐ฝ Single datum
Learning Objectives
๐ฏ Sub-goal: Build a film card component
To break down this problem, we’ll render a single datum, before doing this for the whole list. Here’s one film:
Interactive code block
const film = {
title: "Killing of Flower Moon",
director: "Martin Scoresese"
times: ["15:35"],
certificate: "15",
duration: 112
};
Starting with this object, we’ll focus only on building this section of the user interface:
๐งฑ Composing elements
Learning Objectives
We can start by calling createElement
to create and
title
property from the film
object:
Interactive code block
const film = {
title: "Killing of Flower Moon",
director: "Martin Scoresese"
times: ["15:35"],
certificate: "15",
duration: 112
};
const filmCard = document.createElement("section");
filmCard.innerHTML = `
<h3>${film.title}</h3>
`;
console.log(filmCard);
If we open up the console tab, we should be able to see this element logged in the console. However, it won’t yet appear in the browser.
Appending elements
To display the film card, we need to append it to another element that is already in the DOM tree.
Interactive code block
const film = {
title: "Killing of Flower Moon",
director: "Martin Scoresese"
times: ["15:35"],
certificate: "15",
duration: 112
};
const filmCard = document.createElement("section");
filmCard.innerHTML = `
<p>${film.title}</p>
`;
document.querySelector("ul").append(section);
๐ Building a component
Learning Objectives
Recall our sub-goal:
๐ฏ Sub-goal: Build a film card component
We can render any film object in the user interface with a general component. We’ve composed DOM elements to create a card; now we will build a reusable component. To do this, we wrap up our code inside a JavaScript function. JavaScript functions reuse code: so we can implement reusable UI components using functions.
Look at our code so far:
Interactive code block
const film = {
title: "Killing of Flower Moon",
director: "Martin Scoresese"
times: ["15:35"],
certificate: "15",
duration: 112
};
const filmCard = document.createElement("section");
filmCard.innerHTML = `
<p>${film.title}</p>
`;
console.log(filmCard);
We can wrap up some of this code to create our reusable film card component:
Interactive code block
function createFilmCard(film) {
const card = document.createElement("section");
card.innerHTML = `
<p>${film.title}</p>
`;
return card;
}
const film1 = {
title: "Killing of Flower Moon",
director: "Martin Scorsese"
times: ["15:35"],
certificate: "15",
duration: 112
};
const film2 = {
title: "Typist Artist Pirate King",
director: "Carol Morley"
times: ["15:00", "20:00"],
certificate: "12A",
duration: 108
};
document
.querySelector("ul")
.append(createFilmCard(film1),createFilmCard(film2)); // append the film cards to the DOM
createFilmCard
so it renders other film properties. Other film properties on this object are director
, times
, certificate
and duration
.createFilmCard
function to use object destructuring in the parameters.One to one
Learning Objectives
We can now render any film data object in the UI. However, to fully solve this problem we must render a list of data. For each film object, we need to render a corresponding film object in the UI. In this case, there is a
To create an array of card components, we can iterate through the film data using a for...of
loop:
Interactive code block
const films = [
{
title: "Killing of Flower Moon",
director: "Martin Scoresee"
times: ["15:35"],
certificate: "15",
duration: 112
},
{
title: "Typist Artist Pirate King",
directory: "Carol Morley"
times: ["15:00", "20:00"],
certificate: "12A",
duration: 108
},
];
const filmCards = [];
for (const item of films) {
filmCards.push(createFilmCard(item));
}
document.querySelector("ul").append(...elements);
// invoke append using the spread operator
However, there are alternative methods for building this array of UI components.
๐บ๏ธ Using map
Learning Objectives
For every item in a starting array, we want to apply a function to each element in the starting array to create a new array. Earlier, we used a for...of
statement to apply the function createFilmCard
to each element in the array. However, we can also build an array like this using the map
array method. map
is a
map
. Then map
will use this function to create a new array.
Let’s work through an example:
Interactive code block
const arr = [5, 20, 30];
function double(num) {
return num * 2;
}
Our goal is to create a new array of doubled numbers given this array and function. We want to create the array [10, 40, 60]
. Look, it’s another “one to one mapping”
We are building a new array by applying each item to double
. Each time we call double
we store its return value in a new array:
Interactive code block
function double(num) {
return num * 2;
}
const arr = [5, 20, 30];
const doubledNums = [double(5), double(20), double(30)];
But we want to generalise this. Whenever we are writing out the same thing repeatedly in code, we probably want to make a general rule instead.
We can do this by calling map
:
Interactive code block
function double(num) {
return num * 2;
}
const arr = [5, 20, 30];
const doubledNums = arr.map(double);
Use the array visualiser to observe what happens when map
is used on the arr
. Try changing the elements of arr
and the function that is passed to map
. Answer the following questions in the visualiser:
- What does
map
do? - What does
map
return? - What parameters does the
map
method take? - What parameters does the callback function take?
Play computer with the example to see what happens when the map
is called.
Given the list of film data:
Interactive code block
const films = [
{
title: "Killing of Flower Moon",
director: "Martin Scorsese"
times: ["15:35"],
certificate: "15",
duration: 112
},
{
title: "Typist Artist Pirate King",
director: "Carol Morley"
times: ["15:00", "20:00"],
certificate: "12A",
duration: 108
},
];
Use createFilmCard
and map
to create an array of film card components. In a local project, render this array of components in the browser.
Prep Teamwork Project ๐
Learning Objectives
Preparation
Your team must be defined beforehand and be composed of the following:
- A mix of technical skills/levels
- A mix of genders
- Maximum 5 members
To do so organise yourselves as a cohort.
Tip: try to work with people you haven’t worked with yet.
Introduction
Working as a team when developing software is very important. The collaboration, knowledge sharing and diversity of skill sets result in higher-quality products, faster development cycles, and a more efficient workflow. This delivers success in meeting customer needs and achieving project goals.
During the JS3 module, you will prepare a Teamwork Project. You will be assigned to a team by the volunteers. You will work as a team on a fictional digital product for a fictional client. You are not going to do any coding. This project aims to improve your teamwork skills. You will learn how to get prepared for a product before the software development phase.
You will:
- Week 1: Decide how you are going to work as a team
- Week 2: Define the minimum viable product for your project
- Week 3: Define features and user stories for your product
- Week 4: Present a brief for your product and your teamwork
Each team member should:
- Organise and attend calls
- Have clear responsibilities
- Help each other when needed
Prepare for your team meeting
๐ฏ Goal: Be prepared for the first meeting with your team (30 minutes)
- Prepare for the meeting by reading the Sprint 1-Day Plan yourself. Make notes of questions or important information you think would be essential to discuss with your team.
Have a first meeting with your team
๐ฏ Goal: Understand the teamwork project (60 minutes)
- Organise or attend the first call with your team
- Discuss your understanding of the project, your questions and what you think you as a team should prepare for.
PS: Do NOT discuss the day plan exercises. Otherwise, this will disrupt the lesson.
Understand team roles
๐ฏ Goal: Reflect about team roles (30 minutes)
This is an individual task.
- Read the article “Belbin’s Team Roles” and watch the video.
- Reflect on the team roles you have taken so far in your life.
Keep this article in mind if you need to change your behaviour to help your team perform better during the project.