Skip to content

Commit 0ddce91

Browse files
committed
upd
1 parent ac4a119 commit 0ddce91

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Lecture-11-12-JS-Framework-P4-5/framework.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function hasChanged(a, b) {
22
// Insert whatever logic you prefer.
3+
if (typeof a === "object" && typeof b === "object") {
4+
return true;
5+
}
36
return a !== b;
47
}
58

@@ -259,6 +262,10 @@ export class Component {
259262
return el;
260263
};
261264

265+
div(props = {}, children = []) {
266+
return this.element("div", props, children)
267+
}
268+
262269
render() {
263270
return this.element("div");
264271
}

Lecture-11-12-JS-Framework-P4-5/script.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,42 @@ class Board extends Component {
88
[".",".","O"],
99
["O",".","."],
1010
[".","X","."],
11-
])
11+
]);
12+
13+
this.winner = this.createDerived(() => {
14+
return ".";
15+
}, [this.board])
1216

1317
setTimeout(() => {
1418
// Set the board to any value and observe the change!
1519
this.board.setValue([
1620
[".","O","O"],
1721
["O","X","X"],
1822
["X","X",".", "."],
23+
["."]
1924
])
2025
}, 1000)
2126
}
2227

28+
playTurn(rowIdx, colIdx) {
29+
// some logic
30+
}
31+
2332
render() {
2433
const containerProps = { style: "display: flex; flex-direction: column" };
2534
const rowProps = { style: "display: flex" };
2635
const cellProps = { style: "border: 1px solid black; width: 20px; height: 20px;" };
2736

28-
return this.element("div", containerProps, [
29-
this.map(this.board, {}, function(row) {
30-
return this.element("div", rowProps, [
31-
this.map(row, {}, function(cell) {
32-
const isOccupied = this.createDerived(() => cell.value !== ".", [cell]);
37+
const play_turn_binded = this.playTurn.bind(this);
3338

34-
return this.element("div", cellProps, [
35-
this.condition(isOccupied, {}, function() {
36-
return this.element("div", { textContent: cell })
39+
return this.div(containerProps, [
40+
this.map(this.board, function(row, rowIdx) {
41+
return this.div(rowProps, [
42+
this.map(row, {}, function(cell, colIdx) {
43+
return this.div(cellProps, [
44+
this.div({
45+
textContent: cell,
46+
onclick: () => play_turn_binded(rowIdx, colIdx)
3747
})
3848
]);
3949
})

0 commit comments

Comments
 (0)