1
1
# Spiel Client
2
2
3
- Spiel client is a flexible and light frontend framework to make easier create a complex and modern applications for the client side. It joins two light but powerful libraries: [ Picodom ] ( https://github.com/picodom/picodom ) and [ Navigo] ( https://github.com/krasimir/navigo ) .
3
+ Spiel client is a flexible and light frontend framework to make easier create a complex and modern applications for the client side. It joins two light but powerful libraries: [ ultradom ] ( https://github.com/jorgebucaran/ultradom ) and [ Navigo] ( https://github.com/krasimir/navigo ) .
4
4
5
5
## Api documentation
6
6
* [ Spiel client API] ( https://spieljs.github.io/spiel-client/ )
@@ -13,44 +13,42 @@ Spiel client is a flexible and light frontend framework to make easier create a
13
13
### First config you routes
14
14
15
15
``` typescript
16
- import {ConfigRouters , srouter } from ' spiel-client' ;
17
-
18
- import {genericHooks , hooks } from ' ./hooks' ;
19
-
16
+ import {IConfigRouters } from " spiel-client" ;
17
+ import {genericHooks , hooks } from " ./hooks" ;
20
18
import {page1 , page2 , page3 , page4 , notFound } from ' ./Pages'
21
19
22
- const configSettings: ConfigRouters = {
23
- rootPath: ' http://localhost:9876/' ,
24
- default: ' /home' ,
25
- useHash: true ,
26
- genericHooks: genericHooks ,
27
- defaultProps: ' default' ,
20
+ export const configSettings: IConfigRouters = {
21
+ default: " /home" ,
22
+ defaultProps: " default" ,
23
+ genericHooks ,
24
+ hash: " #!" ,
28
25
notFound: true ,
29
- hash: ' #! ' ,
30
- notFoundPath: ' /not-found ' ,
26
+ notFoundPath: " /not-found " ,
27
+ rootPath: " http://localhost:9876/ " ,
31
28
routers: [{
32
- path: ' /home ' ,
33
- page: page1 ,
29
+ page: Page1 ,
30
+ path: " /home " ,
34
31
routers: [{
35
- path: ' / child/:number ' ,
36
- page: page2 ,
37
- alias: ' child ' ,
38
- hooks: hooks ,
32
+ alias: " child" ,
33
+ hooks ,
34
+ page: Page2 ,
35
+ path: " /child/:number " ,
39
36
routers: [{
40
- path: ' /child2/:word ' ,
41
- alias: ' grandchild ' ,
42
- page: page3
43
- }]
44
- },{
45
- path: ' /brother ' ,
46
- defaultProps: ' my own prop ' ,
47
- page: page4
48
- }]
37
+ alias: " grandchild " ,
38
+ page: Page3 ,
39
+ path: " /child2/:word " ,
40
+ }],
41
+ }, {
42
+ defaultProps: " my own prop " ,
43
+ page: Page4 ,
44
+ path: " /brother " ,
45
+ }],
49
46
},
50
47
{
51
- path: ' /not-found' ,
52
- page: notFound
53
- }]
48
+ page: notFound ,
49
+ path: " /not-found" ,
50
+ }],
51
+ useHash: true ,
54
52
};
55
53
56
54
srouter .setRouters (configDefault ).resolve ();
@@ -77,12 +75,12 @@ router.resolve();
77
75
Assign alias if you want generate links:
78
76
79
77
``` typescript
80
- const configDefault: ConfigRouters = {
78
+ const configDefault: IConfigRouters = {
81
79
routers: [{
82
- path : ' / user/:id ' ,
80
+ alias : ' user'
83
81
default : ' /user'
84
82
page : page1 ,
85
- alias : ' user'
83
+ path : ' / user/:id ' ,
86
84
}]
87
85
};
88
86
@@ -94,75 +92,89 @@ console.log(router.generate('user', {id: 4})); // #/user/4
94
92
Set your generic hooks for all the routes:
95
93
96
94
``` typescript
97
- export const genericHooks = {
95
+ export const genericHooks: IGenericHooks = {
96
+ after : (params : Params ) => {
97
+ if (params && params .number ) {
98
+ params .number = + params .number + 2 ;
99
+ }
100
+ },
98
101
before : (done : (suppress ? : boolean ) => void , params : Params ) => {
99
- if (params && params .number ) params .number = + params .number + 2 ;
102
+ if (params && params .number ) {
103
+ params .number = + params .number + 2 ;
104
+ }
100
105
done ();
101
106
},
102
- after : (params : Params ) => {
103
- if (params && params .number ) params .number = + params .number + 2 ;
104
- }
105
- }
107
+ };
106
108
```
107
109
108
110
And your hooks for expecific route:
109
111
110
112
``` typescript
111
- export const hooks = {
113
+ export const hooks: IHooks = {
114
+ after : (params : Params ) => {
115
+ if (params ) {
116
+ params .number = + params .number + 2 ;
117
+ }
118
+ },
119
+ already : (params : Params ) => {
120
+ if (params ) {
121
+ params .number = + params .number + 2 ;
122
+ }
123
+ },
112
124
before : (done : (suppress ? : boolean ) => void , params : Params ) => {
113
- if (params ) params .number = + params .number + 2 ;
125
+ if (params ) {
126
+ params .number = + params .number + 2 ;
127
+ }
114
128
done ();
115
129
},
116
- after : (params : Params ) => {
117
- if (params ) params .number = + params .number + 2
118
- },
119
130
leave : (params : Params ) => {
120
- if (params ) params .number = + params .number + 2
131
+ if (params ) {
132
+ params .number = + params .number + 2 ;
133
+ }
121
134
},
122
- already : (params : Params ) => {
123
- if (params ) params .number = + params .number + 2
124
- }
125
- }
135
+ };
126
136
```
127
137
### Create your page components
128
138
129
- ``` typescript
130
- import { createNode , Component , Page , State , VNode , Children , JSXElements } from ' spiel-client ' ;
139
+ ``` tsx
140
+ import { Children , createNode , IPage , JSXElements , srouter , State , VNode } from " ../../../src " ;
131
141
132
- interface Show {
133
- value: string
142
+ interface IShow {
143
+ value: string ;
144
+ onGo: () => void ;
134
145
}
135
146
136
- export class Page1 implements Page {
137
- state = {
138
- text: ' This is a Page component'
139
- }
147
+ export class Page5 implements IPage {
148
+ public state = {
149
+ text: " This is a component" ,
150
+ };
140
151
141
- view(state : State ): JSXElements {
152
+ public view(state : State ): JSXElements {
142
153
return (
143
- < Show value = {state.text }>
144
- <span >And this is its component < / span >
154
+ <Show value = { state .text } onGo = { () => srouter . go ( " / " ) } >
155
+ <span >And this is its child </span >
145
156
</Show >
146
- )
157
+ );
147
158
}
148
159
}
149
160
150
- function Show ({value }: Show , children : Children ) {
161
+ function Show({value , onGo }: IShow , children : Children ) {
151
162
return (
152
163
<div >
153
164
<span >{ value } </span >
154
- < div id = ' child' > {children }< / div >
165
+ <div id = " child" >{ children } </div >
166
+ <button id = " go-parent" onclick = { () => srouter .go (" /" )} ></button >
155
167
</div >
156
- )
168
+ );
157
169
}
158
170
159
- export const page1 = new Page1 ();
171
+ export const page5 = new page5 ();
160
172
```
161
173
162
- ** Note:** you need to export the singleton of the class and always import ` h ` and ` Component ` to render the views
174
+ ** Note:** you need to export the singleton of the class and always import ` createNode ` to render the views
163
175
164
- To know more about the views see more in [ picodom ] ( https://github.com/picodom/picodom ) .
165
- All the picodom functionalities like ` patch ` , ` h ` etc... are available in spiel-client
176
+ To know more about the views see more in [ ultradom ] ( https://github.com/jorgebucaran/ultradom ) .
177
+ All the ultradom functionalities like ` patch ` , ` createNode ` etc... are available in spiel-client
166
178
167
179
### Config your project
168
180
@@ -200,33 +212,34 @@ Remember always to put `createNode` in the `jsxFactory` option.
200
212
Create your mocks:
201
213
202
214
``` typescript
203
- import { createNode , Component , Page , State , VNode , Children , JSXElements } from ' spiel-client ' ;
215
+ import { Children , createNode , IPage , JSXElements , State , VNode } from " ../../src " ;
204
216
205
- interface Show {
206
- value: string
217
+ interface IShow {
218
+ value: string ;
207
219
}
208
220
209
- export class ComponentTest implements Page {
210
- state = {
211
- text: ' This is a component'
212
- }
221
+ export class ComponentTest implements IPage {
222
+ public state = {
223
+ text: " This is a component" ,
224
+ };
213
225
214
- view(state : State ): JSXElements {
226
+ public view(state : State ): JSXElements {
215
227
return (
216
228
< Show value = {state.text }>
217
229
<span >And this is its child < / span >
218
230
< / Show >
219
- )
231
+ );
220
232
}
233
+
221
234
}
222
235
223
- function Show ({value }: Show , children : Children ) {
236
+ function Show({value }: IShow , children : Children ) {
224
237
return (
225
238
<div >
226
239
<span >{value }< / span >
227
- < div id = ' child' > {children }< / div >
240
+ < div id = " child" > {children }< / div >
228
241
< / div >
229
- )
242
+ );
230
243
}
231
244
232
245
export const componentTest = new ComponentTest ();
@@ -235,27 +248,27 @@ export const componentTest = new ComponentTest();
235
248
and your file spec:
236
249
237
250
``` typescript
238
- import { createNode as u , VNode } from " spiel-client " ;
239
- import { expect , assert } from " chai " ;
251
+ import { assert , expect } from " chai " ;
252
+ import { createNode as u , patch , VNode } from " ../../src " ;
240
253
241
- import {componentTest } from ' ./mocks' ;
254
+ import {componentTest } from " ./mocks" ;
242
255
243
- describe (' Component' , () => {
244
- let nodes: VNode <any >
245
- before (()=> {
256
+ describe (" Component" , () => {
257
+ let nodes: VNode <any >;
258
+ before (() => {
246
259
nodes = u (componentTest .view , componentTest .state );
247
260
});
248
- it (' has to be created' , ()=> {
249
- const text: any = nodes .children .find ((node : any ) => node .type === ' span' );
250
- expect (text .children [0 ]).has .to .be .equal (' This is a component' )
261
+ it (" has to be created" , () => {
262
+ const text: any = nodes .children .find ((node : any ) => node .nodeName === " span" );
263
+ expect (text .children [0 ]).has .to .be .equal (" This is a component" );
251
264
});
252
265
253
- it (' has to exist its children' , ()=> {
254
- const child: any = nodes .children .find ((node : any ) => node .type === ' div' );
255
- const text: any = child .children .find ((node : any ) => node .type === ' span' );
266
+ it (" has to exist its children" , () => {
267
+ const child: any = nodes .children .find ((node : any ) => node .nodeName === " div" );
268
+ const text: any = child .children .find ((node : any ) => node .nodeName === " span" );
256
269
expect (text .children [0 ]).has .to .be .equal (" And this is its child" );
257
270
});
258
- })
271
+ });
259
272
```
260
273
261
274
## Make compatible with ES5 browsers
@@ -290,7 +303,7 @@ tsconfig.json:
290
303
In your code:
291
304
``` typescript
292
305
import ' es6-shim' ; // or every polyfill which you need
293
- import { srouter , ConfigRouters } from ' spiel-client' ;
306
+ import { srouter , IConfigRouters } from ' spiel-client' ;
294
307
```
295
308
296
309
## Run Spiel Client tests
0 commit comments