lec-js-07
JP incommunicado
March 15th: Withdraw Deadline
April 5th: Project Submission
April 11th: Final Exam
March 13th: Midterm 2
If you're feeling anonymousy:
◉ What is "Web Storage"?
◉ How do you put things in it?
◉ How do you take things out of it?
◉ What requirements are unclear?
◉ What sticking points are there?
◉ Where's the low-hanging fruit? The time-consuming fruit?
◉ Some suggestions for the codez.
This is through the Web Storage API.
Unlike cookies, which do the remora thing with request/response headers, Web Storage is limited strictly to the client side. This means it's tricky for the server side to manipulate Web Storage without some kind of script shenanigans.
What's the difference? 🤔
Saving things in storage is blessedly easy.
localStorage.setItem("foo", "bar");
localStorage.setItem("size", 3);
localStorage.setItem("isHappy", true);
localStorage.setItem(3, 4);What is the data type of the stored value?
What is the data type of the stored value?
What is the data type of the stored key?
⚠️ The devtools are being a bit disingenuous when displaying the storage contents.
How do we turn JS objects into strings again?🤔
let gameResult = {
player: "Max",
score: 12
};
localStorage.setItem("result", JSON.stringify(gameResult));let gameResults = [
{name: "Max", score: 12},
{name: "Aki", score: 17}
];
localStorage.setItem("results", JSON.stringify(gameResults));let cashier = {
empId: 731823,
startDate: new Date('2024-03-09'),
daysEmployed: function() {
let timeDiff = new Date() - this.startDate;
return timeDiff / (1000 * 60 * 60 * 24);
}
}What will be in local storage in this case?🤔
localStorage.setItem("cashier", JSON.stringify(cashier));Does a Movie object need methods?
Retrieving things from Web Storage is also easy!
localStorage.setItem("foo", "bar");
localStorage.getItem("foo");
localStorage.setItem("size", 3);
localStorage.getItem("size");
localStorage.setItem("isHappy", true);
localStorage.getItem("isHappy");
localStorage.setItem(3, 4);
localStorage.getItem(3);What is the data type of the retrieved value?
What is the data type of the retrieved value?
What is the data type of the retrieved value?
How would you turn the retrieved values to their original types? 🤔
How do we turn JSON into JS objects again?🤔
let gameResult = {
player: "Max",
score: 12
};
localStorage.setItem("result", JSON.stringify(gameResult));
JSON.parse(localStorage.getItem("result"));let gameResults = [
{name: "Max", score: 12},
{name: "Aki", score: 17}
];
localStorage.setItem("results", JSON.stringify(gameResults));
JSON.parse(localStorage.getItem("results"));let cashier = {
empId: 731823,
startDate: new Date('2024-03-09'),
daysEmployed: function() {
let timeDiff = new Date() - this.startDate;
return timeDiff / (1000 * 60 * 60 * 24);
}
}What will resulting object look like in this case?🤔
localStorage.setItem("cashier", JSON.stringify(cashier));
JSON.parse(localStorage.getItem("cashier"));These are just suggestions.
I think they're useful suggestion...but they're just suggestions.
const commonElements = {
movieTitle: document.querySelector("some selector"),
searchInitiator: document.querySelector("some selector"),
sortTitleAscInitiator: document.querySelector("some selector"),
etc....
}
export { commonElements };import { commonElements } from "./some/path";
commonElements.searchInitiator.addEventListener("click", someHandler);function handleIncreasePlayerCount() {
// This doesn't use the triggering event.
}
function handleDropPlayer(clickEvent) {
// This one is currently reacting to a click.
}
function handleSearchTermEntry(inputEvent) {
// This one is currently reacting to an input.
}import {
commonElements
} from "./blah/blah";
import {
handleIncreasePlayerCount,
handleDropPlayer
} from "./blah/blah";
function registerEventHandlers() {
commonElements
.incPlayerCountInitiator
.addEventHandler("click", handleIncreasePlayerCount);
commonElements
.dropPlayerInitiator
.addEventHandler("click", handleDropPlayer);
}
export { registerEventHandlers };import { registerEventHandlers } from "./blah/blah";
registerEventHandlers();