Skip to content

Commit 83b886b

Browse files
committed
Implement counter
1 parent 158f6e5 commit 83b886b

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

README.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ https://tsoding.org/grecha.js/example.html
1919
const kasha = img("Kasha.png");
2020
const kashaHard = img("KashaHard.gif");
2121
22-
entry.appendChild(
23-
router({
24-
"/": div(
25-
h1("Grecha.js"),
26-
div(a("Foo").att$("href", "#/foo")),
27-
div(a("Bar").att$("href", "#/bar")),
28-
div(kasha).onclick$(function () {
29-
if (this.contains(kasha)) {
30-
kasha.replaceWith(kashaHard);
31-
} else {
32-
kashaHard.replaceWith(kasha);
33-
}
34-
}),
35-
),
36-
"/foo": div(
37-
h1("Foo"),
38-
p(LOREM),
39-
div(a("Home").att$("href", "#")),
40-
),
41-
"/bar": div(
42-
h1("Bar"),
43-
p(LOREM),
44-
div(a("Home").att$("href", "#"))
45-
)
46-
})
47-
);
22+
let count = 0;
23+
let hard = false;
24+
const r = router({
25+
"/": () => div(
26+
h1("Grecha.js"),
27+
div(a("Foo").att$("href", "#/foo")),
28+
div(a("Bar").att$("href", "#/bar")),
29+
div("Counter: "+count),
30+
div(hard ? kashaHard : kasha).onclick$(function () {
31+
count += 1;
32+
hard = !hard
33+
r.refresh();
34+
}),
35+
),
36+
"/foo": () => div(
37+
h1("Foo"),
38+
p(LOREM),
39+
div(a("Home").att$("href", "#")),
40+
),
41+
"/bar": () => div(
42+
h1("Bar"),
43+
p(LOREM),
44+
div(a("Home").att$("href", "#"))
45+
)
46+
});
47+
entry.appendChild(r);
4848
</script>
4949
</body>
5050
</html>

example.html

+27-27
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@
77
<script>
88
const kasha = img("Kasha.png");
99
const kashaHard = img("KashaHard.gif");
10-
11-
entry.appendChild(
12-
router({
13-
"/": div(
14-
h1("Grecha.js"),
15-
div(a("Foo").att$("href", "#/foo")),
16-
div(a("Bar").att$("href", "#/bar")),
17-
div(kasha).onclick$(function () {
18-
if (this.contains(kasha)) {
19-
kasha.replaceWith(kashaHard);
20-
} else {
21-
kashaHard.replaceWith(kasha);
22-
}
23-
}),
24-
),
25-
"/foo": div(
26-
h1("Foo"),
27-
p(LOREM),
28-
div(a("Home").att$("href", "#")),
29-
),
30-
"/bar": div(
31-
h1("Bar"),
32-
p(LOREM),
33-
div(a("Home").att$("href", "#"))
34-
)
35-
})
36-
);
10+
11+
let count = 0;
12+
let hard = false;
13+
const r = router({
14+
"/": () => div(
15+
h1("Grecha.js"),
16+
div(a("Foo").att$("href", "#/foo")),
17+
div(a("Bar").att$("href", "#/bar")),
18+
div("Counter: "+count),
19+
div(hard ? kashaHard : kasha).onclick$(function () {
20+
count += 1;
21+
hard = !hard
22+
r.refresh();
23+
}),
24+
),
25+
"/foo": () => div(
26+
h1("Foo"),
27+
p(LOREM),
28+
div(a("Home").att$("href", "#")),
29+
),
30+
"/bar": () => div(
31+
h1("Bar"),
32+
p(LOREM),
33+
div(a("Home").att$("href", "#"))
34+
)
35+
});
36+
entry.appendChild(r);
3737
</script>
3838
</body>
3939
</html>

grecha.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ function router(routes) {
5656
while (result.firstChild) {
5757
result.removeChild(result.lastChild);
5858
}
59-
result.appendChild(routes[hashLocation]);
59+
result.appendChild(routes[hashLocation]());
6060

6161
return result;
6262
};
63-
6463
syncHash();
6564
// TODO(#3): there is way to "destroy" an instance of the router to make it remove it's "hashchange" callback
6665
window.addEventListener("hashchange", syncHash);
66+
result.refresh = syncHash;
6767

6868
return result;
6969
}

0 commit comments

Comments
 (0)