@@ -48,17 +48,16 @@ Here are the different package that make Tram-One possible...
48
48
49
49
For Rendering:
50
50
- [ hyperx] ( https://github.com/substack/hyperx )
51
- - [ bel] ( https://github.com/shama /bel )
51
+ - [ bel-create-element ] ( https://github.com/JRJurman /bel-create-element )
52
52
- [ rbel] ( https://github.com/aaaristo/rbel )
53
+ - [ nanomorph] ( https://github.com/choojs/nanomorph )
53
54
54
55
For Routing:
55
- - [ nanorouter ] ( https://github.com/choojs/nanorouter )
56
+ - [ rlite ] ( https://github.com/chrisdavies/rlite )
56
57
- [ url-listener] ( https://github.com/JRJurman/url-listener )
57
58
58
59
For State Management:
59
- - [ nanomorph] ( https://github.com/choojs/nanomorph )
60
- - [ minidux] ( https://github.com/freeman-lab/minidux )
61
- - [ xtend] ( https://github.com/Raynos/xtend )
60
+ - [ hover-engine] ( https://github.com/JRJurman/hover-engine )
62
61
63
62
While not used in this project, Tram-One is heavily inspired by the
64
63
[ choo] ( https://github.com/choojs/choo ) view framework.
@@ -67,6 +66,17 @@ creator, [Yoshua Wuyts](https://github.com/yoshuawuyts).
67
66
If you like some of the things here, definitely
68
67
[ go check out that project] ( https://github.com/choojs ) .
69
68
69
+ ## Size
70
+ ```
71
+ ┌──────────────────────────────────────────────────┐
72
+ │ │
73
+ │ Module size: 3.42 KB, Gzipped size: 1.34 KB │
74
+ │ │
75
+ │ UMD size: 23.75 KB, Gzipped size: 7.96 KB │
76
+ │ │
77
+ └──────────────────────────────────────────────────┘
78
+ ```
79
+
70
80
## Video Tutorial
71
81
This video tutorial goes through the process of build a Tram-One web app
72
82
from start to finish.
@@ -136,28 +146,24 @@ const cHtml = Tram.html({
136
146
' color-button' : colorElement
137
147
})
138
148
139
- // create a reducer, that handles changing the color of the app
140
- const colorReducer = (state , action ) => {
141
- switch (action .type ) {
142
- case (' SET_COLOR' ):
143
- return action .color
144
- default :
145
- return state // you must ALWAYS return the state by default
146
- }
149
+ // create a set of actions, that handles changing the color of the app
150
+ const colorActions = {
151
+ init : () => ' blue' ,
152
+ setColor : (currentColor , newColor ) => newColor
147
153
}
148
154
149
155
// home page to load on the main route
150
- const home = (state ) => {
156
+ const home = (store , actions ) => {
151
157
152
158
// actionCreator that dispatches to the reducer
153
159
const onSetColor = (color ) => () => {
154
- state . dispatch ({type : ' SET_COLOR ' , color} )
160
+ actions . setColor ( color)
155
161
}
156
162
157
163
// we use cHtml so that we have color-button available in the template
158
164
return cHtml `
159
165
<div >
160
- I think the best color for this wall is... ${ state .color } !
166
+ I think the best color for this wall is... ${ store .color } !
161
167
or maybe it's...
162
168
<color-button onclick =${onSetColor( ' blue' )} >blue</color-button >
163
169
<color-button onclick =${onSetColor( ' red' )} >red</color-button >
@@ -166,22 +172,12 @@ const home = (state) => {
166
172
`
167
173
}
168
174
169
- // page to render on the unmatched routes (which by default go to 404)
170
- const noPage = (state ) => {
171
- return cHtml `
172
- <div >
173
- <h1 >404!</h1 >
174
- Sorry pal, no page here...
175
- </div >
176
- `
177
- }
178
-
179
175
// add routes, by using path matchers with function components
180
176
app .addRoute (' /' , home)
181
177
app .addRoute (' /404' , noPage)
182
178
183
- // add reducer, map all state values to ' color', and set the initial value to 'blue'
184
- app .addReducer ( ' color' , colorReducer, ' blue ' )
179
+ // add actions and save the state as ` color` on the store
180
+ app .addActions ({ color: colorActions} )
185
181
app .start (' .main' )
186
182
```
187
183
@@ -201,7 +197,7 @@ Tram-One has a simple interface to help build your web app.
201
197
202
198
### ` Tram.html([registry]) `
203
199
_ Reference: [ hyperx] ( https://github.com/substack/hyperx ) ,
204
- [ bel] ( https://github.com/shama /bel ) ,
200
+ [ bel-create-element ] ( https://github.com/JRJurman /bel-create-element ) ,
205
201
[ rbel] ( https://github.com/aaaristo/rbel ) _
206
202
207
203
` Tram.html ` returns a function that can be used to transform
@@ -243,7 +239,7 @@ const html = Tram.html({
243
239
' wrap' : pageWraper
244
240
})
245
241
246
- const home = (state ) => {
242
+ const home = () => {
247
243
return html `
248
244
<wrap >
249
245
This is my shiny app!
@@ -281,21 +277,13 @@ app.addRoute('/', home)
281
277
282
278
</details >
283
279
284
- ### ` app.addReducer(key, reducer, state ) `
285
- _ Reference: [ minidux ] ( https://github.com/freeman-lab/minidux ) _
280
+ ### ` app.addActions(actionGroups ) `
281
+ _ Reference: [ hover-engine ] ( https://github.com/JRJurman/hover-engine ) _
286
282
287
- ` app.addReducer ` adds a reducer onto the current instance of Tram.
288
- It takes in three arguments:<br >
289
- ` key ` , which is where the state will be exposed,<br >
290
- ` reducer ` , the function that updates state,<br >
291
- ` state ` , the initial state of the reducer.
292
-
293
- Note, ` state ` here will be exposed in the views as ` state[key] ` .
294
-
295
- The ` reducer ` should be a function, that takes in ` state ` , and an ` action ` .<br >
296
- ` state ` can be anything you want, a number, object, whatever. At the end of the
297
- reducer, you should ALWAYS return this by default.<br >
298
- ` action ` should be an object, with a ` type ` property.
283
+ ` app.addActions ` adds a set of actions that can be triggered in the instance of Tram-One.
284
+ It takes in one argument, an object where:<br >
285
+ the keys are values that can be pulled in the view<br >
286
+ the values are actions that can be triggered in the view<br >
299
287
300
288
<details >
301
289
<summary >
@@ -307,26 +295,21 @@ Example:
307
295
const app = new Tram ()
308
296
const html = Tram .html ()
309
297
310
- // in this example, state is a number (the votes)
298
+ // in this example, `vote` is a number
311
299
// but in a larger app, this could be an object
312
300
// with multiple key-value pairs
313
- const counterReducer = (state , action ) => {
314
- switch (action .type ) {
315
- case (' UP' ):
316
- return state + 1
317
- case (' DOWN' ):
318
- return state - 1
319
- default :
320
- return state
321
- }
301
+ const voteActions = {
302
+ init : () => 0 ,
303
+ up : (vote ) => vote + 1 ,
304
+ down : (vote ) => vote - 1
322
305
}
323
306
324
- const home = (state ) => {
307
+ const home = (state , actions ) => {
325
308
const upvote = () => {
326
- state . dispatch ({type : ' UP ' } )
309
+ actions . up ( )
327
310
}
328
311
const downvote = () => {
329
- state . dispatch ({type : ' DOWN ' } )
312
+ actions . down ( )
330
313
}
331
314
332
315
return html `
@@ -338,21 +321,21 @@ const home = (state) => {
338
321
`
339
322
}
340
323
341
- app .addReducer ( ' votes' , counterReducer, 0 )
324
+ app .addActions ({ votes: voteActions} )
342
325
```
343
326
344
327
</details >
345
328
346
329
### ` app.addRoute(path, page) `
347
- _ Reference: [ nanorouter ] ( https://github.com/yoshuawuyts/nanorouter ) _
330
+ _ Reference: [ rlite ] ( https://github.com/chrisdavies/rlite ) _
348
331
349
332
` app.addRoute ` will associate a component with a route.<br >
350
333
` path ` should be a matchable route for the application. Look up
351
- [ nanorouter ] ( https://github.com/yoshuawuyts/nanorouter )
334
+ [ rlite ] ( https://github.com/chrisdavies/rlite )
352
335
to see all the possible options here.<br >
353
- ` page ` should be a function that takes in a ` state ` object for the entire app .
336
+ ` page ` should be a function that takes in a ` store ` , ` actions ` and ` params ` .
354
337
355
- The state passed into ` page ` will have any path parameters for the route as well .
338
+ The ` params ` object passed into the ` page ` function will have any path parameters and query params .
356
339
357
340
<details >
358
341
<summary >
@@ -364,20 +347,20 @@ Example:
364
347
const app = new Tram ()
365
348
const html = Tram .html ()
366
349
367
- const homePage = (state ) => {
350
+ const homePage = () => {
368
351
return html ` <div >This is my shiny app!</div >`
369
352
}
370
353
371
- const colorPage = (state ) => {
354
+ const colorPage = (store , actions , params ) => {
372
355
const style = `
373
- background: ${ state .color } ;
356
+ background: ${ params .color } ;
374
357
width: 100px;
375
358
height: 100px;
376
359
`
377
360
return html ` <div style =${style } ></div >`
378
361
}
379
362
380
- const noPage = (state ) => {
363
+ const noPage = () => {
381
364
return html ` <div >Oh no! We couldn't find what you were looking for</div >`
382
365
}
383
366
@@ -388,23 +371,10 @@ app.addRoute('/404', noPage)
388
371
389
372
</details >
390
373
391
- ### ` app.dispatch(action) `
392
- _ Reference: [ minidux] ( https://github.com/freeman-lab/minidux ) _
393
-
394
- ** WARNING: EXPERIMENTAL METHOD** <br >
395
- _ This method is currently under discussion:<br >
396
- https://github.com/JRJurman/tram-one/issues/8 ._
397
-
398
- ` app.dispatch ` will dispatch an action to the combined reducers. This should
399
- ** only be used outside of components** . When inside of a component, you have
400
- access to ` state.dispatch ` . ` app.dispatch ` should only be used when you need to
401
- dispatch an action in testing.
402
- ` action ` should be an object with a property ` type ` .
403
-
404
374
### ` app.start(selector, [pathName]) `
405
375
406
- ` app.start ` will kick off the app. Once this is called, all the reducers
407
- are combined, and the app is mounted onto the ` selector ` .<br >
376
+ ` app.start ` will kick off the app. Once this is called the app is mounted onto the
377
+ ` selector ` .<br >
408
378
` selector ` can be a node or a css selector (which is fed into
409
379
` document.querySelector ` ).<br >
410
380
` pathName ` can be an initial path, if you don't want to check the browser's
@@ -449,29 +419,29 @@ app.start('.main')
449
419
450
420
</details >
451
421
452
- ### ` app.mount(selector, pathName, state ) `
422
+ ### ` app.mount(selector, pathName, store, actions ) `
453
423
** WARNING: INTENDED FOR INTERNAL USE ONLY**
454
424
455
- ` app.mount ` matches a route from ` pathName ` , passes in a ` state ` object,
425
+ ` app.mount ` matches a route from ` pathName ` , passes in a ` store ` and ` actions ` object,
456
426
and either creates a child div, or updates a child div under ` selector ` .
457
427
458
428
This was created to clean up the code in the library, but may be useful for
459
429
testing.
460
430
461
431
** YOU SHOULD NEVER CALL THIS DIRECTLY FOR YOUR APP**
462
432
463
- ### ` app.toNode(pathName, [state ]) `
433
+ ### ` app.toNode(pathName[, store, actions ]) `
464
434
465
- ` app.toNode ` returns a HTMLNode of the app for a given route and state . The
466
- function matches a route from ` pathName ` , and either takes in a ` state ` , or
467
- uses the default state (that's been created by adding reducers).
435
+ ` app.toNode ` returns a HTMLNode of the app for a given route and store . The
436
+ function matches a route from ` pathName ` , and either takes in a ` store ` , or
437
+ uses the default store (that's been created by adding reducers).
468
438
469
439
While initially created to clean up the code in the library, this can be useful
470
440
if you want to manually attach the HTMLNode that Tram-One builds to whatever.
471
441
472
- ### ` app.toString(pathName, [state ]) `
442
+ ### ` app.toString(pathName[, store ]) `
473
443
474
- ` app.toString ` returns a string of the app for a given route and state . It has
444
+ ` app.toString ` returns a string of the app for a given route and store . It has
475
445
the same interface at ` app.toNode ` , and basically just calls ` .outerHTML ` (or
476
446
` toString ` on the server) on the node.
477
447
0 commit comments