structured-fw 0.8.41 → 0.8.42
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 +92 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -347,13 +347,25 @@ app.request.on('GET', '/home', async (ctx) => {
|
|
|
347
347
|
});
|
|
348
348
|
```
|
|
349
349
|
|
|
350
|
+
> [!TIP]
|
|
351
|
+
> Since version 0.8.4 Document extends EventEmitter, and "componentCreated" event is emitted whenever a component instance is created within the Document.\
|
|
352
|
+
> This makes the following possible:
|
|
353
|
+
> ```
|
|
354
|
+
> app.on('documentCreated', (doc) => {
|
|
355
|
+
> doc.on('componentCreated', (component) => {
|
|
356
|
+
> // do something with the document or the component
|
|
357
|
+
> })
|
|
358
|
+
> })
|
|
359
|
+
> ```
|
|
360
|
+
|
|
350
361
|
## Component
|
|
351
|
-
A component is comprised of 1-3 files. It always must include one HTML file, while server side and client side files are optional.
|
|
362
|
+
A component is comprised of [1-3 files](#component-parts). It always must include one HTML file, while server side and client side files are optional.
|
|
352
363
|
* HTML file probably requires no explanation
|
|
353
364
|
* server side file, code that runs on the server and makes data available to HTML and client side code
|
|
354
365
|
* client side file, code that runs on the client (in the browser)
|
|
355
366
|
|
|
356
|
-
|
|
367
|
+
> [!TIP]
|
|
368
|
+
> You should never need to instantiate a Component on your own. You will always load a Component representing your page into a document (using `Document.loadComponent(componentName: string)`), which will know what to do from there.
|
|
357
369
|
|
|
358
370
|
Example component files:
|
|
359
371
|
- `/app/views/`
|
|
@@ -373,6 +385,12 @@ It is recommended, but not necessary, that you contain each component in it's ow
|
|
|
373
385
|
- Components HTML file can have a `.hbs` extension (which allows for better Handlebars syntax highlighting)
|
|
374
386
|
- Components can reside at any depth in the file structure
|
|
375
387
|
|
|
388
|
+
### Component parts
|
|
389
|
+
- [Component HTML](#component-html) (_ComponentName.html_)
|
|
390
|
+
- [Component server-side code](#component-server-side-code) (_ComponentName.ts_)
|
|
391
|
+
- [Component client-side code](#component-client-side-code) (_ComponentName.client.ts_)
|
|
392
|
+
|
|
393
|
+
### Component HTML
|
|
376
394
|
Let's create a HelloWorld Component `/app/views/HelloWorld/HelloWorld.html`:\
|
|
377
395
|
`Hello, World!`
|
|
378
396
|
|
|
@@ -392,7 +410,12 @@ export default function(app: Application) {
|
|
|
392
410
|
You can now run the app and if you open /hello/world in the browser you will see:\
|
|
393
411
|
`Hello, World!` - which came from your HelloWorld component.
|
|
394
412
|
|
|
395
|
-
|
|
413
|
+
> [!TIP]
|
|
414
|
+
> It is recommended to use .hbs (Handlebars) extension as you will get better syntax highlighting in most IDEs. Other than syntax highlighting there is no difference between using html or hbs extension.
|
|
415
|
+
|
|
416
|
+
That was the simplest possible example, let's make it more interesting by adding some server-side code.
|
|
417
|
+
|
|
418
|
+
### Component server-side code
|
|
396
419
|
Create a new file `/app/views/HelloWorld/HelloWorld.ts` (server side component code):
|
|
397
420
|
```
|
|
398
421
|
import { ComponentScaffold } from 'system/Types.js';
|
|
@@ -426,7 +449,15 @@ Your lucky number is [a number from 0-100]
|
|
|
426
449
|
This demonstrates the use of a *server side component code* to make data available to HTML.
|
|
427
450
|
We just generated a random number, but the data could be anything and will more often come from a database, session, or be provided by the parent component.
|
|
428
451
|
|
|
452
|
+
> [!IMPORTANT]
|
|
453
|
+
> Server side `getData` will receive the following arguments:
|
|
454
|
+
> - `data: LooseObject` any data passed in (either by attributes, ClientComponent.add or ClientComponent.redraw)
|
|
455
|
+
> - `ctx: RequestContext` - current `RequestContext`, you will often use this to access for example ctx.data (`RequestContextData`) or ctx.sessionId to interact with session
|
|
456
|
+
> - `app: Application` - your Application instance. You can use it to, for example, access the session in combination with ctx.sessionId
|
|
457
|
+
|
|
429
458
|
Let's make it even more interesting by adding some client side code to it.
|
|
459
|
+
|
|
460
|
+
### Component client-side code
|
|
430
461
|
Create `/app/views/HelloWorld/HelloWorld.client.ts`:
|
|
431
462
|
```
|
|
432
463
|
import { InitializerFunction } from 'system/Types.js';
|
|
@@ -509,12 +540,6 @@ What about client side? **By default, data returned by server side code is not a
|
|
|
509
540
|
> [!NOTE]
|
|
510
541
|
> Whenever a component with server-side code is rendered, `getData` is automatically called and anything it returns is available in HTML. You can export all returned data to client-side code by setting `exportData = true` or you can export some of the fields by setting `exportFields = ["field1", "field2", ...]` as a direct property of the class. To access the exported data from client-side use `ClientComponent`.`getData(key: string)` which will be `this.getData(key:string)` within client side code.
|
|
511
542
|
|
|
512
|
-
> [!IMPORTANT]
|
|
513
|
-
> Server side `getData` will receive the following arguments:
|
|
514
|
-
> - `data: LooseObject` any data passed in (either by attributes, ClientComponent.add or ClientComponent.redraw)
|
|
515
|
-
> - `ctx: RequestContext` - current `RequestContext`, you will often use this to access for example ctx.data (`RequestContextData`) or ctx.sessionId to interact with session
|
|
516
|
-
> - `app: Application` - your Application instance. You can use it to, for example, access the session in combination with ctx.sessionId
|
|
517
|
-
|
|
518
543
|
Let's create a client side code for `AnotherComponent` and export the `betterNumber` to it, create `/app/views/AnotherComponent/AnotherComponent.client.ts`:
|
|
519
544
|
```
|
|
520
545
|
import { InitializerFunction } from 'system/Types.js';
|
|
@@ -611,13 +636,67 @@ Methods:
|
|
|
611
636
|
- `query(componentName: string, recursive: boolean = true): Array<ClientComponent>` - return all components with given name found within this component, if `recursive = false`, only direct children are considered
|
|
612
637
|
- `ref<T>(refName: string): T` - get a HTMLElement or ClientComponent that has attribute `ref="[refName]"`
|
|
613
638
|
- `arrayRef<T>(refName: string): Array<T>` - get an array of HTMLElement or ClientComponent that have attribute `array:ref="[refName]"`
|
|
614
|
-
- `add(appendTo: HTMLElement, componentName: string, data?: LooseObject)
|
|
639
|
+
- `add(appendTo: HTMLElement, componentName: string, data?: LooseObject): Promise<ClientComponent | null>` - add `componentName` component to `appendTo` element, optionally passing `data` to the component when it's being rendered. Returns a promise that resolves with added ClientComponent or null if something went wrong
|
|
640
|
+
- `redraw(data?: LooseObject): Promise<void>` - redraw the component, optionally provide data which will be available server side
|
|
641
|
+
|
|
642
|
+
### Conditionals
|
|
643
|
+
You can make any DOM node within your components conditionally shown/hidden using `data-if` attribute.\
|
|
644
|
+
For example:
|
|
645
|
+
```
|
|
646
|
+
<div data-if="showDiv"></div>
|
|
647
|
+
```
|
|
648
|
+
Above div will only be shown if store.showDiv = true
|
|
649
|
+
|
|
650
|
+
You can also use `!` to invert the value, `!showDiv` in which case div would be shown if showDiv is false.
|
|
651
|
+
|
|
652
|
+
You can also use comparison:
|
|
653
|
+
```
|
|
654
|
+
<div data-if="val === 1"></div>
|
|
655
|
+
<div data-if="val == 1"></div>
|
|
656
|
+
<div data-if="val !== 1"></div>
|
|
657
|
+
<div data-if="val != 1"></div>
|
|
658
|
+
<div data-if="val > 1"></div>
|
|
659
|
+
<div data-if="val < 1"></div>
|
|
660
|
+
<div data-if="val <= 1"></div>
|
|
661
|
+
<div data-if="val >= 1"></div>
|
|
662
|
+
```
|
|
663
|
+
|
|
664
|
+
The right hand side of the comparison does not have to be boolean or number. It can be a string or any primitive value, but the numeric comparisons don't make sense in such case.
|
|
665
|
+
|
|
666
|
+
You can also define callbacks and use them as the condition, in you ComponentName.client.ts:
|
|
667
|
+
```
|
|
668
|
+
import { InitializerFunction } from 'structured-fw/Types';
|
|
669
|
+
export const init: InitializerFunction = async function() {
|
|
670
|
+
this.conditionalCallback('showDiv', () => {
|
|
671
|
+
// return a boolean here
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
```
|
|
675
|
+
|
|
676
|
+
then in ComponentName.html:
|
|
677
|
+
```
|
|
678
|
+
<div data-if="showDiv()"></div>
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
**Basic animation/transitions**\
|
|
682
|
+
If you use conditionals on any DOM node, you may also enable basic animations/transitions using following attributes:
|
|
683
|
+
- Enable transition:
|
|
684
|
+
- `data-transition-show-slide="durationMilliseconds"` - when DOM node is shown, slide it in
|
|
685
|
+
- `data-transition-hide-slide="durationMilliseconds"` - when DOM node is hidden, slide it out
|
|
686
|
+
- `data-transition-show-fade="durationMilliseconds"` - fade DOM node in
|
|
687
|
+
- `data-transition-hide-fade="durationMilliseconds"` - fade DOM node out
|
|
688
|
+
- Modify transition (slide only)
|
|
689
|
+
- `data-transform-origin-show="CSS transform origin"` - from where does the component slide in for example `0% 50%` to slide it in from mid-left
|
|
690
|
+
- `data-transform-origin-hide="CSS transform origin"` - where does the component slide out to for example `100% 100%` to slide it out to bottom-right
|
|
691
|
+
- `data-transition-axis-show="X | Y"` - slide animation axis
|
|
692
|
+
- `data-transition-axis-hide="X | Y"` - slide animation axis
|
|
615
693
|
|
|
616
694
|
## Good to know
|
|
617
695
|
- [Using CSS frameworks](#css-frameworks)
|
|
618
696
|
- [Using JS runtimes other than Node.js](#runtimes)
|
|
619
697
|
- [Why not JSR](#jsr)
|
|
620
698
|
- [Best practices](#best-practices)
|
|
699
|
+
- [Having an issue?](#issues-and-feedback)
|
|
621
700
|
|
|
622
701
|
### CSS frameworks
|
|
623
702
|
We rarely write all CSS from scratch, usually we use a CSS framework to speed us up. Structured allows you to work with any CSS frameworks such as Tailwind, PostCSS or Bootstrap.
|
|
@@ -762,6 +841,9 @@ If you ran `npx structured init`, it has created /app/models for you. Structured
|
|
|
762
841
|
|
|
763
842
|
You can create additional code separation, for example, it would make sense to have /app/lib for code that interfaces an API, or have /app/Util.ts where you export utility functions. Structured boilerplate does not include these as not all applications will need them.
|
|
764
843
|
|
|
844
|
+
### Issues and feedback
|
|
845
|
+
If you have any issues with the framework or the npm package, please don't hesitate to open an issue on [github](https://github.com/julijan/structured). Feedback is also welcome!
|
|
846
|
+
|
|
765
847
|
## Why Structured
|
|
766
848
|
Framework was developed by someone who has been a web developer for almost 20 years (me), and did not like the path web development has taken.
|
|
767
849
|
\
|
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.42",
|
|
18
18
|
"scripts": {
|
|
19
19
|
"develop": "tsc --watch",
|
|
20
20
|
"startDev": "cd build && nodemon --watch '../app/**/*' --watch '../build/**/*' -e js,html,css index.js",
|