structured-fw 0.8.43 → 0.8.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,17 @@ npm install @types/node
|
|
|
41
41
|
### Create boilerplate
|
|
42
42
|
`npx structured init`
|
|
43
43
|
|
|
44
|
+
### Create a test route
|
|
45
|
+
Create a file `/app/routes/Test.ts`:
|
|
46
|
+
```
|
|
47
|
+
import { Application } from 'structured-fw/Application';
|
|
48
|
+
export default function(app: Application) {
|
|
49
|
+
app.request.on('GET', '/test', async()=> {
|
|
50
|
+
return 'Hello, World!';
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
44
55
|
### Compile
|
|
45
56
|
`tsc`\
|
|
46
57
|
This will create a directory `build` (or whatever you have in tsconfig.json as compilerOptions.outputDir)
|
|
@@ -57,6 +68,8 @@ cd build
|
|
|
57
68
|
pm2 start index.js --name="[appName]"
|
|
58
69
|
```
|
|
59
70
|
|
|
71
|
+
If you followed the above steps, you should be able to access `http://localhost:9191/test` in your browser and see the output `Hello, World!`.
|
|
72
|
+
|
|
60
73
|
# Key concepts
|
|
61
74
|
|
|
62
75
|
## Application
|
|
@@ -418,7 +431,7 @@ That was the simplest possible example, let's make it more interesting by adding
|
|
|
418
431
|
### Component server-side code
|
|
419
432
|
Create a new file `/app/views/HelloWorld/HelloWorld.ts` (server side component code):
|
|
420
433
|
```
|
|
421
|
-
import { ComponentScaffold } from '
|
|
434
|
+
import { ComponentScaffold } from 'structured-fw/Types';
|
|
422
435
|
export default class HelloWorld implements ComponentScaffold {
|
|
423
436
|
async getData(): Promise<{
|
|
424
437
|
luckyNumber: number
|
|
@@ -460,7 +473,7 @@ Let's make it even more interesting by adding some client side code to it.
|
|
|
460
473
|
### Component client-side code
|
|
461
474
|
Create `/app/views/HelloWorld/HelloWorld.client.ts`:
|
|
462
475
|
```
|
|
463
|
-
import { InitializerFunction } from '
|
|
476
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
464
477
|
export const init: InitializerFunction = async function() {
|
|
465
478
|
const generateNew = this.ref<HTMLButtonElement>('newNumber');
|
|
466
479
|
|
|
@@ -509,7 +522,7 @@ Parent says your lucky number is {{number}}.
|
|
|
509
522
|
That's it. Since `AnotherComponent` has no server side code, all data passed to it is exported to HTML, hence the `number` you passed from `HelloWorld` will be readily available for use. If AnotherComponent had a server side part, the process is a bit different, it will receive it as part of the `data`, but can choose whether to make it available to the HTML, or just make use of it and return other stuff. Let's see how that works.
|
|
510
523
|
Create `/app/views/AnotherComponent/AnotherComponent.ts`:
|
|
511
524
|
```
|
|
512
|
-
import { ComponentScaffold } from '
|
|
525
|
+
import { ComponentScaffold } from 'structured-fw/Types';
|
|
513
526
|
export default class AnotherComponent implements ComponentScaffold {
|
|
514
527
|
async getData(data: { number: number }): Promise<{
|
|
515
528
|
parentSuggests: number,
|
|
@@ -542,7 +555,7 @@ What about client side? **By default, data returned by server side code is not a
|
|
|
542
555
|
|
|
543
556
|
Let's create a client side code for `AnotherComponent` and export the `betterNumber` to it, create `/app/views/AnotherComponent/AnotherComponent.client.ts`:
|
|
544
557
|
```
|
|
545
|
-
import { InitializerFunction } from '
|
|
558
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
546
559
|
export const init: InitializerFunction = async function() {
|
|
547
560
|
const betterNumber = this.getData<number>('betterNumber');
|
|
548
561
|
|
|
@@ -552,7 +565,7 @@ export const init: InitializerFunction = async function() {
|
|
|
552
565
|
|
|
553
566
|
And let's update `AnotherComponent.ts` to export `betterNumber`:
|
|
554
567
|
```
|
|
555
|
-
import { ComponentScaffold } from '
|
|
568
|
+
import { ComponentScaffold } from 'structured-fw/Types';
|
|
556
569
|
export default class AnotherComponent implements ComponentScaffold {
|
|
557
570
|
exportFields = ['betterNumber'];
|
|
558
571
|
async getData(data: { number: number }): Promise<{
|
|
@@ -574,7 +587,7 @@ This concept is wrong to start with, if we want a component to be independent, i
|
|
|
574
587
|
|
|
575
588
|
Let's say we wanted to access the `parent` Component from `AnotherComponent`:
|
|
576
589
|
```
|
|
577
|
-
import { InitializerFunction } from '
|
|
590
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
578
591
|
export const init: InitializerFunction = async function() {
|
|
579
592
|
const betterNumber = this.getData<number>('betterNumber');
|
|
580
593
|
|
|
@@ -585,7 +598,7 @@ Here we accessed the `parent` and obtained it's `name`.
|
|
|
585
598
|
|
|
586
599
|
*"But we did not send any data to the parent here"* - correct, we did not, and we won't, instead we can inform them we have some data available, or that an event they might be interested in has occurred, and if they care, so be it:
|
|
587
600
|
```
|
|
588
|
-
import { InitializerFunction } from '
|
|
601
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
589
602
|
export const init: InitializerFunction = async function() {
|
|
590
603
|
const betterNumber = this.getData<number>('betterNumber');
|
|
591
604
|
|
|
@@ -595,7 +608,7 @@ export const init: InitializerFunction = async function() {
|
|
|
595
608
|
|
|
596
609
|
We emitted an `event` with `eventName` = "`truth`" and a `payload`, which in this case is a string, but can be of any type. If the parent cares about it (or for that matter, not necessarily the parent, but anyone in the component tree), they can subscribe to that event. Let's subscribe to the event from `HelloWorld` (`HelloWorld.client.ts`):
|
|
597
610
|
```
|
|
598
|
-
import { InitializerFunction } from '
|
|
611
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
599
612
|
export const init: InitializerFunction = async function() {
|
|
600
613
|
|
|
601
614
|
const child = this.find('AnotherComponent'); // ClientComponent | null
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"main": "build/index",
|
|
17
|
-
"version": "0.8.
|
|
17
|
+
"version": "0.8.44",
|
|
18
18
|
"scripts": {
|
|
19
19
|
"develop": "tsc --watch",
|
|
20
20
|
"startDev": "cd build && nodemon --watch '../app/**/*' --watch '../build/**/*' -e js,html,css index.js",
|