little more from the mvc chapter

This commit is contained in:
Geir Okkenhaug Jerstad 2024-08-27 13:11:59 +02:00
parent ed7c48e877
commit cafa3b8957
6 changed files with 87 additions and 27 deletions

32
MVC/MVC_quizz.html Normal file
View file

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Model View Controller</title>
<style>
</style>
</head>
<body>
<div id="app"></div>
<script>
// Model
let questionIndex = 0;
let points = 0;
// view
updateView();
function updateView(){
let question;
if (questionIndex == 0) question = "Hva er 2 + 2 ?"
document.getElementById('app').innerHTML = /*HTML*/ `
<h1></h1>
Poeng: ${points}<br>
<input type="text">
`;
};
</script>
</body>
</html>

View file

@ -8,55 +8,57 @@
</head> </head>
<body> <body>
<div id="app">
</div>
<script>
// Model
let firstName = "...her kommer et navn";
let place = "..her kommer et sted";
let food = "...her kommer en matrett";
// View
updateView();
function updateView() {
let html = `
<div class="group"> <div class="group">
<div>Hei jeg heter</div> <div>Hei jeg heter</div>
<div id="namediv">...her kommer et navn<br></div> <div id="namediv"> ${firstName}<br></div>
</div> </div>
<div class="group"> <div class="group">
<div>Jeg bor i</div> <div>Jeg bor i</div>
<div id="placediv">..her kommer et sted<br></div> <div id="placediv"> ${place}<br></div>
</div> </div>
<div class="group"> <div class="group">
<div>Min favorittmat er</div> <div>Min favorittmat er</div>
<div id="fooddiv">...her kommer en matrett<br></div> <div id="fooddiv"> ${food}<br></div>
</div> </div>
<button onclick="setFields()">Klikk her for å sett inn verdier</button> <button onclick="setFields()">Klikk her for å sett inn verdier</button>
Navn: <input type="text" onchange="setName(this.value)"> Navn: <input type="text" onchange="setName(this.value)">
Sted: <input type="text" onchange="setPlace(this.value)"> Sted: <input type="text" onchange="setPlace(this.value)">
Mat: <input type="text" onchange="setFood(this.value)"> Mat: <input type="text" onchange="setFood(this.value)">
`;
<script> document.getElementById('app').innerHTML = html;
// Model
let firstName;
let place;
let food;
// View
function setFields() {
document.getElementById("namediv").innerHTML = firstName;
document.getElementById("placediv").innerHTML = place;
document.getElementById("fooddiv").innerHTML = food;
} }
// Controller // Controller
function setName(nameInput) { function setName(nameInput) {
firstName = nameInput; firstName = nameInput;
setFields(); updateView();
} }
function setPlace(placeInput) { function setPlace(placeInput) {
place = placeInput; place = placeInput;
setFields(); updateView();
} }
function setFood(foodInput) { function setFood(foodInput) {
food = foodInput; food = foodInput;
setFields(); updateView();
} }
</script> </script>
<style> <style>
.group { .group {
display: flex; display: flex;
padding: 1px; padding: 3px;
} }
</style> </style>
</body> </body>

View file

@ -1,11 +1,16 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Document</title>
<link rel="stylesheet" href="style.css">
</head> </head>
<body>
<body>
<div id="app"></div>
<script src="script.js"></script>
</body> </body>
</html> </html>

View file

@ -0,0 +1,21 @@
// Model Variabler
let sentence = ""
let words = ["Green", "Red", "Cat", "Man", "Started", "Jumped", "Pool", "Wall"];
// View
// HTML, onclick CSS
updateView();
function updateView() {
document.getElementById('app').innerHTML = 'Dette er ____';
document.getElementById('app').innerHTML = /*HTML*/ `
<button>${words[0]}</button><button>${words[1]}</button>
<button>${words[2]}</button><button>${words[3]}</button>
<button>${words[4]}</button><button>${words[5]}</button>
<button>${words[6]}</button><button>${words[7]}</button>
`;
};
// Controller endrer modellen
function insertWords() {
}

View file

View file

@ -7,6 +7,6 @@
</head> </head>
<body> <body>
<div>Thus spake the Master Programmer:<br> <div>Thus spake the Master Programmer:<br>
"After three days without programming, life becomes meaningless."</div> "After three days without programming, life becomes meaningless." - <a href="https://www.mit.edu/~xela/tao.html">The Tao of Programming</a></div>
</body> </body>
</html> </html>