File tree 3 files changed +55
-55
lines changed
3 files changed +55
-55
lines changed Original file line number Diff line number Diff line change @@ -19,32 +19,32 @@ https://tsoding.org/grecha.js/example.html
19
19
const kasha = img (" Kasha.png" );
20
20
const kashaHard = img (" KashaHard.gif" );
21
21
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 );
48
48
</script >
49
49
</body >
50
50
</html >
Original file line number Diff line number Diff line change 7
7
< script >
8
8
const kasha = img ( "Kasha.png" ) ;
9
9
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 ) ;
37
37
</ script >
38
38
</ body >
39
39
</ html >
Original file line number Diff line number Diff line change @@ -56,14 +56,14 @@ function router(routes) {
56
56
while ( result . firstChild ) {
57
57
result . removeChild ( result . lastChild ) ;
58
58
}
59
- result . appendChild ( routes [ hashLocation ] ) ;
59
+ result . appendChild ( routes [ hashLocation ] ( ) ) ;
60
60
61
61
return result ;
62
62
} ;
63
-
64
63
syncHash ( ) ;
65
64
// TODO(#3): there is way to "destroy" an instance of the router to make it remove it's "hashchange" callback
66
65
window . addEventListener ( "hashchange" , syncHash ) ;
66
+ result . refresh = syncHash ;
67
67
68
68
return result ;
69
69
}
You can’t perform that action at this time.
0 commit comments