@dooboostore/simple-web-component 1.0.29 → 1.0.30
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 +432 -27
- package/dist/cjs/decorators/elementDefine.js +2 -0
- package/dist/cjs/decorators/elementDefine.js.map +2 -2
- package/dist/cjs/decorators/emitCustomEvent.js.map +2 -2
- package/dist/cjs/decorators/index.js +1 -0
- package/dist/cjs/decorators/index.js.map +2 -2
- package/dist/cjs/decorators/setProperty.js +97 -0
- package/dist/cjs/decorators/setProperty.js.map +7 -0
- package/dist/cjs/elements/SwcAppMixin.js +1 -1
- package/dist/cjs/elements/SwcAppMixin.js.map +2 -2
- package/dist/cjs/elements/SwcAsync.js +4 -1
- package/dist/cjs/elements/SwcAsync.js.map +2 -2
- package/dist/cjs/elements/SwcChoose.js +54 -38
- package/dist/cjs/elements/SwcChoose.js.map +2 -2
- package/dist/cjs/elements/register.js +131 -39
- package/dist/cjs/elements/register.js.map +2 -2
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/utils/Utils.js +11 -8
- package/dist/cjs/utils/Utils.js.map +2 -2
- package/dist/esm/decorators/elementDefine.js +2 -0
- package/dist/esm/decorators/elementDefine.js.map +2 -2
- package/dist/esm/decorators/emitCustomEvent.js.map +2 -2
- package/dist/esm/decorators/index.js +1 -0
- package/dist/esm/decorators/index.js.map +2 -2
- package/dist/esm/decorators/setProperty.js +78 -0
- package/dist/esm/decorators/setProperty.js.map +7 -0
- package/dist/esm/elements/SwcAppMixin.js +1 -1
- package/dist/esm/elements/SwcAppMixin.js.map +2 -2
- package/dist/esm/elements/SwcAsync.js +4 -1
- package/dist/esm/elements/SwcAsync.js.map +2 -2
- package/dist/esm/elements/SwcChoose.js +54 -38
- package/dist/esm/elements/SwcChoose.js.map +2 -2
- package/dist/esm/elements/register.js +131 -39
- package/dist/esm/elements/register.js.map +2 -2
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/utils/Utils.js +11 -8
- package/dist/esm/utils/Utils.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +87 -8
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
- package/dist/types/decorators/elementDefine.d.ts.map +1 -1
- package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
- package/dist/types/decorators/index.d.ts +1 -0
- package/dist/types/decorators/index.d.ts.map +1 -1
- package/dist/types/decorators/setProperty.d.ts +13 -0
- package/dist/types/decorators/setProperty.d.ts.map +1 -0
- package/dist/types/elements/SwcAsync.d.ts.map +1 -1
- package/dist/types/elements/SwcChoose.d.ts +1 -1
- package/dist/types/elements/SwcChoose.d.ts.map +1 -1
- package/dist/types/elements/register.d.ts +3 -1
- package/dist/types/elements/register.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/Utils.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +87 -8
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
- package/package.json +1 -1
- package/src/decorators/elementDefine.ts +4 -0
- package/src/decorators/emitCustomEvent.ts +1 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/setProperty.ts +97 -0
- package/src/elements/SwcAppMixin.ts +2 -2
- package/src/elements/SwcAsync.ts +5 -1
- package/src/elements/SwcChoose.ts +76 -37
- package/src/elements/register.ts +158 -39
- package/src/index.ts +3 -1
- package/src/utils/Utils.ts +15 -11
package/README.md
CHANGED
|
@@ -17,8 +17,7 @@ Register Web Components with automatic lifecycle management and DI support.
|
|
|
17
17
|
@elementDefine('my-component')
|
|
18
18
|
class MyComponent extends HTMLElement {
|
|
19
19
|
@onInitialize
|
|
20
|
-
|
|
21
|
-
super();
|
|
20
|
+
onconstructor(service: MyService) {
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
```
|
|
@@ -30,10 +29,9 @@ Inject services into Web Components using the `@onInitialize` decorator.
|
|
|
30
29
|
@elementDefine('dashboard')
|
|
31
30
|
class Dashboard extends HTMLElement {
|
|
32
31
|
@onInitialize
|
|
33
|
-
|
|
32
|
+
onconstructor(
|
|
34
33
|
@Inject({ symbol: UserService.SYMBOL }) userService: UserService
|
|
35
34
|
) {
|
|
36
|
-
super();
|
|
37
35
|
this.userService = userService;
|
|
38
36
|
}
|
|
39
37
|
}
|
|
@@ -220,8 +218,7 @@ Bind attributes on host element itself.
|
|
|
220
218
|
productId: string = '';
|
|
221
219
|
|
|
222
220
|
@onInitialize
|
|
223
|
-
|
|
224
|
-
super();
|
|
221
|
+
onconstructor() {
|
|
225
222
|
if (this.productId) {
|
|
226
223
|
this.loadProduct(this.productId);
|
|
227
224
|
}
|
|
@@ -241,39 +238,65 @@ onDisconnected() {
|
|
|
241
238
|
// Called when element leaves DOM
|
|
242
239
|
}
|
|
243
240
|
|
|
244
|
-
@onAttributeChanged
|
|
245
|
-
onAttributeChanged(name: string, oldValue: string, newValue: string) {
|
|
246
|
-
// Called when attribute changes
|
|
247
|
-
}
|
|
248
241
|
|
|
249
242
|
@onConnectedSwcApp
|
|
250
|
-
onAppReady(
|
|
243
|
+
onAppReady(router: Router) {
|
|
251
244
|
// Called after SwcApp.connect() completes on all already-connected components
|
|
252
245
|
// Full DI support available through hostSet
|
|
253
246
|
console.log('Application initialization complete!');
|
|
254
247
|
}
|
|
255
248
|
```
|
|
256
249
|
|
|
257
|
-
### 8. **Routing with
|
|
258
|
-
|
|
250
|
+
### 8. **Declarative Routing with swc-choose**
|
|
251
|
+
Combine `@subscribeSwcAppRouteChange` with `swc-choose` for clean, declarative routing without boilerplate.
|
|
259
252
|
|
|
260
253
|
```typescript
|
|
261
254
|
@elementDefine('root-router')
|
|
262
255
|
class RootRouter extends HTMLElement {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
256
|
+
private router: Router;
|
|
257
|
+
|
|
258
|
+
@onInitialize
|
|
259
|
+
onconstructor(router: Router) {
|
|
260
|
+
this.router = router;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
@setProperty('#router', 'value')
|
|
264
|
+
@subscribeSwcAppRouteChange(['/', '/product/{id}'])
|
|
265
|
+
routeChanged(router: RouterEventType) {
|
|
266
|
+
return router;
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
@
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
269
|
+
@onConnectedInnerHtml
|
|
270
|
+
render() {
|
|
271
|
+
return `
|
|
272
|
+
<template id="router" is="swc-choose">
|
|
273
|
+
<!-- Home -->
|
|
274
|
+
<template is="swc-when" value="{{ ['', '/'].includes($value?.path) }}">
|
|
275
|
+
<home-page/>
|
|
276
|
+
</template>
|
|
277
|
+
|
|
278
|
+
<!-- Product Detail -->
|
|
279
|
+
<template is="swc-when" value="{{ $value?.path.startsWith('/product/') }}">
|
|
280
|
+
<product-page product-id="{{$value?.pathData?.id}}"/>
|
|
281
|
+
</template>
|
|
282
|
+
|
|
283
|
+
<!-- Not Found -->
|
|
284
|
+
<template is="swc-otherwise">
|
|
285
|
+
<not-found-page/>
|
|
286
|
+
</template>
|
|
287
|
+
</template>
|
|
288
|
+
`;
|
|
273
289
|
}
|
|
274
290
|
}
|
|
275
291
|
```
|
|
276
292
|
|
|
293
|
+
**Benefits:**
|
|
294
|
+
- ✅ **No imperative route methods** - routes are declarative templates
|
|
295
|
+
- ✅ **Automatic re-rendering** - attributes like `product-id` update automatically
|
|
296
|
+
- ✅ **Performance optimization** - use `skip-if-same` on templates to prevent re-renders
|
|
297
|
+
- ✅ **Dynamic path parameters** - use `{{$value?.pathData?.id}}` syntax to inject route params
|
|
298
|
+
- ✅ **Cleaner DX** - single unified routing pattern
|
|
299
|
+
|
|
277
300
|
### 9. **Accommodation Pattern**
|
|
278
301
|
Factory-based component registration with explicit DI.
|
|
279
302
|
|
|
@@ -287,8 +310,7 @@ export default (w: Window) => {
|
|
|
287
310
|
@elementDefine(tagName, { window: w })
|
|
288
311
|
class MyComponent extends w.HTMLElement {
|
|
289
312
|
@onInitialize
|
|
290
|
-
|
|
291
|
-
super();
|
|
313
|
+
onconstructor(service: MyService) {
|
|
292
314
|
this.service = service;
|
|
293
315
|
}
|
|
294
316
|
}
|
|
@@ -314,6 +336,390 @@ appElement.connect({
|
|
|
314
336
|
});
|
|
315
337
|
```
|
|
316
338
|
|
|
339
|
+
### 10. **Quickstart - Basic SPA Setup**
|
|
340
|
+
|
|
341
|
+
Complete example of setting up a production SPA with routing, services, and components.
|
|
342
|
+
|
|
343
|
+
#### Step 1. HTML Entry Point
|
|
344
|
+
```html
|
|
345
|
+
<!DOCTYPE html>
|
|
346
|
+
<html>
|
|
347
|
+
<head>
|
|
348
|
+
<meta charset="UTF-8">
|
|
349
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
350
|
+
<title>My SPA</title>
|
|
351
|
+
<!-- Web Components polyfill for older browsers -->
|
|
352
|
+
<script src="https://unpkg.com/@ungap/custom-elements"></script>
|
|
353
|
+
</head>
|
|
354
|
+
<body id="app" is="swc-app-body">
|
|
355
|
+
<!-- Root router will render pages here -->
|
|
356
|
+
<root-router></root-router>
|
|
357
|
+
</body>
|
|
358
|
+
<script src="bundle.js"></script>
|
|
359
|
+
</html>
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
#### Step 2. Services (Dependency Injection)
|
|
363
|
+
```typescript
|
|
364
|
+
// services/UserService.ts
|
|
365
|
+
import { Sim } from '@dooboostore/simple-boot';
|
|
366
|
+
import { ConstructorType } from '@dooboostore/core';
|
|
367
|
+
|
|
368
|
+
export namespace UserService {
|
|
369
|
+
export const SYMBOL = Symbol('UserService');
|
|
370
|
+
|
|
371
|
+
export interface User {
|
|
372
|
+
id: number;
|
|
373
|
+
name: string;
|
|
374
|
+
email: string;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const users: User[] = [
|
|
378
|
+
{ id: 1, name: 'Alice', email: 'alice@example.com' },
|
|
379
|
+
{ id: 2, name: 'Bob', email: 'bob@example.com' }
|
|
380
|
+
];
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export interface UserService {
|
|
384
|
+
getUser(id: number): UserService.User | undefined;
|
|
385
|
+
getAllUsers(): UserService.User[];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Factory function: accepts container and returns service class
|
|
389
|
+
export default (container: symbol): ConstructorType<any> => {
|
|
390
|
+
@Sim({ symbol: UserService.SYMBOL, container: container })
|
|
391
|
+
class UserServiceImpl implements UserService {
|
|
392
|
+
getUser(id: number) {
|
|
393
|
+
return UserService.users.find(u => u.id === id);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
getAllUsers() {
|
|
397
|
+
return [...UserService.users];
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return UserServiceImpl;
|
|
402
|
+
};
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
// services/index.ts
|
|
407
|
+
import userServiceFactory from './UserService';
|
|
408
|
+
|
|
409
|
+
export const serviceFactories = [userServiceFactory];
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
#### Step 3. Router Configuration (Declarative with swc-choose)
|
|
413
|
+
```typescript
|
|
414
|
+
// pages/RootRouter.ts
|
|
415
|
+
import { elementDefine, subscribeSwcAppRouteChange, setProperty, onConnectedInnerHtml, onInitialize } from '@dooboostore/simple-web-component';
|
|
416
|
+
import { Router, RouterEventType } from '@dooboostore/core-web';
|
|
417
|
+
|
|
418
|
+
const rootRouterFactory = (w: Window) => {
|
|
419
|
+
const tagName = 'root-router';
|
|
420
|
+
const existing = w.customElements.get(tagName);
|
|
421
|
+
if (existing) return tagName;
|
|
422
|
+
|
|
423
|
+
@elementDefine(tagName, { window: w })
|
|
424
|
+
class RootRouter extends w.HTMLElement {
|
|
425
|
+
private router: Router;
|
|
426
|
+
|
|
427
|
+
@onInitialize
|
|
428
|
+
onconstructor(router: Router) {
|
|
429
|
+
this.router = router;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
@setProperty('#router', 'value')
|
|
433
|
+
@subscribeSwcAppRouteChange(['/', '/users', '/users/{id}'])
|
|
434
|
+
routeChanged(route: RouterEventType) {
|
|
435
|
+
return route;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
navigate(path: string): void {
|
|
439
|
+
this.router?.go(path);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
@onConnectedInnerHtml
|
|
443
|
+
render() {
|
|
444
|
+
return `
|
|
445
|
+
<template id="router" is="swc-choose">
|
|
446
|
+
<!-- Home Page -->
|
|
447
|
+
<template is="swc-when" value="{{ ['', '/'].includes($value?.path) }}">
|
|
448
|
+
<home-page/>
|
|
449
|
+
</template>
|
|
450
|
+
|
|
451
|
+
<!-- Users List -->
|
|
452
|
+
<template is="swc-when" value="{{ $value?.path === '/users' }}">
|
|
453
|
+
<users-list-page/>
|
|
454
|
+
</template>
|
|
455
|
+
|
|
456
|
+
<!-- User Detail -->
|
|
457
|
+
<template is="swc-when" value="{{ $value?.path.startsWith('/users/') }}">
|
|
458
|
+
<user-detail-page user-id="{{$value?.pathData?.id}}"/>
|
|
459
|
+
</template>
|
|
460
|
+
|
|
461
|
+
<!-- Not Found -->
|
|
462
|
+
<template is="swc-otherwise">
|
|
463
|
+
<not-found-page/>
|
|
464
|
+
</template>
|
|
465
|
+
</template>
|
|
466
|
+
`;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return tagName;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
export default rootRouterFactory;
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
#### Step 4. Page Components
|
|
477
|
+
```typescript
|
|
478
|
+
// pages/HomePage.ts
|
|
479
|
+
import { elementDefine, onConnectedInnerHtml } from '@dooboostore/simple-web-component';
|
|
480
|
+
|
|
481
|
+
const HomePage = (w: Window) => {
|
|
482
|
+
const tagName = 'home-page';
|
|
483
|
+
const existing = w.customElements.get(tagName);
|
|
484
|
+
if (existing) return tagName;
|
|
485
|
+
|
|
486
|
+
@elementDefine(tagName, { window: w })
|
|
487
|
+
class HomePage extends w.HTMLElement {
|
|
488
|
+
@onConnectedInnerHtml({ useShadow: true })
|
|
489
|
+
render() {
|
|
490
|
+
return `
|
|
491
|
+
<style>
|
|
492
|
+
:host { display: block; padding: 20px; }
|
|
493
|
+
h1 { color: #333; }
|
|
494
|
+
a { color: #0066cc; text-decoration: none; margin-right: 20px; }
|
|
495
|
+
</style>
|
|
496
|
+
<h1>Welcome to My SPA</h1>
|
|
497
|
+
<nav>
|
|
498
|
+
<a href="/users">View All Users</a>
|
|
499
|
+
</nav>
|
|
500
|
+
`;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return tagName;
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
export default HomePage;
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
// pages/UsersListPage.ts
|
|
512
|
+
import { elementDefine, onConnectedInnerHtml, onConnectedSwcApp } from '@dooboostore/simple-web-component';
|
|
513
|
+
import { Inject } from '@dooboostore/simple-boot';
|
|
514
|
+
import { UserService } from '../services/UserService';
|
|
515
|
+
|
|
516
|
+
const UsersListPage = (w: Window) => {
|
|
517
|
+
const tagName = 'users-list-page';
|
|
518
|
+
const existing = w.customElements.get(tagName);
|
|
519
|
+
if (existing) return tagName;
|
|
520
|
+
|
|
521
|
+
@elementDefine(tagName, { window: w })
|
|
522
|
+
class UsersListPage extends w.HTMLElement {
|
|
523
|
+
private users: any[] = [];
|
|
524
|
+
|
|
525
|
+
@onConnectedSwcApp
|
|
526
|
+
onconstructor(@Inject({ symbol: UserService.SYMBOL }) userService: UserService) {
|
|
527
|
+
this.users = userService.getAllUsers();
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
@onConnectedInnerHtml({ useShadow: true })
|
|
531
|
+
render() {
|
|
532
|
+
const usersList = this.users
|
|
533
|
+
.map(u => `<li><a href="/users/${u.id}">${u.name}</a></li>`)
|
|
534
|
+
.join('');
|
|
535
|
+
|
|
536
|
+
return `
|
|
537
|
+
<style>
|
|
538
|
+
:host { display: block; padding: 20px; }
|
|
539
|
+
h1 { color: #333; }
|
|
540
|
+
ul { list-style: none; padding: 0; }
|
|
541
|
+
li { margin: 10px 0; }
|
|
542
|
+
a { color: #0066cc; text-decoration: none; }
|
|
543
|
+
</style>
|
|
544
|
+
<h1>Users</h1>
|
|
545
|
+
<ul>${usersList}</ul>
|
|
546
|
+
<a href="/">Back to Home</a>
|
|
547
|
+
`;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return tagName;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
export default UsersListPage;
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
#### Step 4-1. Reusable Components
|
|
558
|
+
```typescript
|
|
559
|
+
// components/UserCard.ts
|
|
560
|
+
import { elementDefine, onConnectedInnerHtml, attributeHost } from '@dooboostore/simple-web-component';
|
|
561
|
+
|
|
562
|
+
const UserCard = (w: Window) => {
|
|
563
|
+
const tagName = 'user-card';
|
|
564
|
+
const existing = w.customElements.get(tagName);
|
|
565
|
+
if (existing) return tagName;
|
|
566
|
+
|
|
567
|
+
@elementDefine(tagName, { window: w })
|
|
568
|
+
class UserCard extends w.HTMLElement {
|
|
569
|
+
@attributeHost('data-username')
|
|
570
|
+
username: string;
|
|
571
|
+
|
|
572
|
+
@attributeHost('data-email')
|
|
573
|
+
email: string;
|
|
574
|
+
|
|
575
|
+
@onConnectedInnerHtml({ useShadow: true })
|
|
576
|
+
render() {
|
|
577
|
+
return `
|
|
578
|
+
<style>
|
|
579
|
+
:host { display: block; }
|
|
580
|
+
.card { border: 1px solid #ddd; border-radius: 4px; padding: 12px; }
|
|
581
|
+
.name { font-weight: bold; font-size: 14px; }
|
|
582
|
+
.email { color: #666; font-size: 12px; }
|
|
583
|
+
</style>
|
|
584
|
+
<div class="card">
|
|
585
|
+
<div class="name">${this.username}</div>
|
|
586
|
+
<div class="email">${this.email}</div>
|
|
587
|
+
</div>
|
|
588
|
+
`;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
return tagName;
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
export default UserCard;
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
#### Step 5. Export Factories
|
|
599
|
+
|
|
600
|
+
**services/index.ts** - Export all service factories
|
|
601
|
+
```typescript
|
|
602
|
+
// services/index.ts
|
|
603
|
+
import userServiceFactory from './UserService';
|
|
604
|
+
|
|
605
|
+
export const serviceFactories = [userServiceFactory];
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
**components/index.ts** - Export all component factories
|
|
609
|
+
```typescript
|
|
610
|
+
// components/index.ts
|
|
611
|
+
import UserCard from './UserCard';
|
|
612
|
+
|
|
613
|
+
export const componentFactories = [
|
|
614
|
+
UserCard,
|
|
615
|
+
];
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
**pages/index.ts** - Export all page factories with router
|
|
619
|
+
```typescript
|
|
620
|
+
// pages/index.ts
|
|
621
|
+
import rootRouterFactory from './RootRouter';
|
|
622
|
+
import HomePage from './HomePage';
|
|
623
|
+
import UsersListPage from './UsersListPage';
|
|
624
|
+
import UserDetailPage from './UserDetailPage';
|
|
625
|
+
|
|
626
|
+
export const pageFactories = [
|
|
627
|
+
rootRouterFactory, // Router first
|
|
628
|
+
HomePage,
|
|
629
|
+
UsersListPage,
|
|
630
|
+
UserDetailPage
|
|
631
|
+
];
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
#### Step 6. Application Bootstrap
|
|
635
|
+
```typescript
|
|
636
|
+
// bootFactory.ts
|
|
637
|
+
import { register } from '@dooboostore/simple-web-component';
|
|
638
|
+
import { serviceFactories } from './services';
|
|
639
|
+
import { componentFactories } from './components';
|
|
640
|
+
import { pageFactories } from './pages';
|
|
641
|
+
|
|
642
|
+
export default (w: Window, container: symbol) => {
|
|
643
|
+
// Initialize services with container (dependency injection)
|
|
644
|
+
// Each factory receives the container symbol and registers its service in the DI container
|
|
645
|
+
serviceFactories.forEach(serviceFactory => serviceFactory(container));
|
|
646
|
+
|
|
647
|
+
// Register pages and components
|
|
648
|
+
register(w, [...pageFactories, ...componentFactories]);
|
|
649
|
+
};
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
#### Step 7. Application Entry Point
|
|
653
|
+
```typescript
|
|
654
|
+
// index.ts
|
|
655
|
+
import 'reflect-metadata';
|
|
656
|
+
import { SwcAppInterface } from '@dooboostore/simple-web-component';
|
|
657
|
+
import bootFactory from './bootFactory';
|
|
658
|
+
|
|
659
|
+
const w = window;
|
|
660
|
+
|
|
661
|
+
w.document.addEventListener('DOMContentLoaded', async () => {
|
|
662
|
+
const container = Symbol('app');
|
|
663
|
+
|
|
664
|
+
// Initialize services and components
|
|
665
|
+
bootFactory(w, container);
|
|
666
|
+
|
|
667
|
+
// Wait for customized built-in element to be defined
|
|
668
|
+
await w.customElements.whenDefined('swc-app-body');
|
|
669
|
+
|
|
670
|
+
// Get app root element
|
|
671
|
+
const appElement = w.document.querySelector('#app') as SwcAppInterface;
|
|
672
|
+
|
|
673
|
+
if (appElement && typeof appElement.connect === 'function') {
|
|
674
|
+
// Connect SPA with routing and DI
|
|
675
|
+
appElement.connect({
|
|
676
|
+
path: '/',
|
|
677
|
+
routeType: 'path',
|
|
678
|
+
container: container,
|
|
679
|
+
window: w,
|
|
680
|
+
onEngineStarted: () => {
|
|
681
|
+
console.log('🚀 Application started successfully');
|
|
682
|
+
// Root router automatically initialized via @elementDefine
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
} else {
|
|
686
|
+
console.error('Failed to initialize SPA');
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
#### Resulting Application Structure
|
|
692
|
+
```
|
|
693
|
+
src/
|
|
694
|
+
├── index.ts # Entry point
|
|
695
|
+
├── index.html # HTML root with <root-router>
|
|
696
|
+
├── bootFactory.ts # Bootstrap services & components via register()
|
|
697
|
+
├── services/
|
|
698
|
+
│ ├── UserService.ts # Service factory (export default)
|
|
699
|
+
│ └── index.ts # Export serviceFactories
|
|
700
|
+
├── pages/
|
|
701
|
+
│ ├── RootRouter.ts # Router factory (export default)
|
|
702
|
+
│ ├── HomePage.ts # Page factory (export default)
|
|
703
|
+
│ ├── UsersListPage.ts # Page factory (export default)
|
|
704
|
+
│ ├── UserDetailPage.ts # Page factory (export default)
|
|
705
|
+
│ └── index.ts # Export pageFactories
|
|
706
|
+
└── components/
|
|
707
|
+
├── UserCard.ts # Component factory (export default)
|
|
708
|
+
└── index.ts # Export componentFactories
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
**Key Flow:**
|
|
712
|
+
1. HTML loads → `<root-router>` element already in DOM
|
|
713
|
+
2. `index.ts` executes → calls `bootFactory`
|
|
714
|
+
3. `bootFactory` registers services and components (including `rootRouterFactory`)
|
|
715
|
+
4. App waits for `swc-app-body` to be defined
|
|
716
|
+
5. `appElement.connect()` initializes routing & DI
|
|
717
|
+
6. `<root-router>` becomes active with `@subscribeSwcAppRouteChange` + `swc-choose` template
|
|
718
|
+
7. Route changes automatically trigger `swc-choose` re-evaluation and template re-rendering
|
|
719
|
+
8. Template attributes like `user-id="{{$value?.pathData?.id}}"` are dynamically populated with route params
|
|
720
|
+
9. All components have access to injected services via `@onInitialize` or `@onConnectedSwcApp`
|
|
721
|
+
|
|
722
|
+
|
|
317
723
|
---
|
|
318
724
|
|
|
319
725
|
## ⚠️ Critical Rules
|
|
@@ -323,14 +729,14 @@ appElement.connect({
|
|
|
323
729
|
|
|
324
730
|
```typescript
|
|
325
731
|
// ✅ CORRECT
|
|
326
|
-
@Sim
|
|
732
|
+
@Sim
|
|
327
733
|
export class UserService { }
|
|
328
734
|
|
|
329
735
|
@elementDefine(tagName, { window: w })
|
|
330
736
|
class UserWidget extends w.HTMLElement { }
|
|
331
737
|
|
|
332
738
|
// ❌ WRONG
|
|
333
|
-
@Sim
|
|
739
|
+
@Sim
|
|
334
740
|
@elementDefine(tagName, { window: w })
|
|
335
741
|
class UserWidget extends w.HTMLElement { }
|
|
336
742
|
```
|
|
@@ -353,8 +759,7 @@ export default (w: Window) => {
|
|
|
353
759
|
```typescript
|
|
354
760
|
// ✅ CORRECT
|
|
355
761
|
@onInitialize
|
|
356
|
-
|
|
357
|
-
super();
|
|
762
|
+
onconstructor(@Inject({ symbol: Service.SYMBOL }) service: Service) {
|
|
358
763
|
this.service = service;
|
|
359
764
|
}
|
|
360
765
|
|
|
@@ -66,6 +66,7 @@ const ensureInit = (inst) => {
|
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
68
|
const hostSet = import_Utils.SwcUtils.getHostSet(inst);
|
|
69
|
+
const appHosts = import_Utils.SwcUtils.findAllParentAppHostsDirect(inst);
|
|
69
70
|
inst._executeSwcScript?.("swc-on-constructor", hostSet);
|
|
70
71
|
const cMethods = (0, import_lifecycles.findAllLifecycleMetadata)(inst, import_lifecycles.ON_INITIALIZE_METADATA_KEY);
|
|
71
72
|
if (cMethods) {
|
|
@@ -410,6 +411,7 @@ const elementDefine = (name, config = {}) => (constructor) => {
|
|
|
410
411
|
this._executeSwcScript("swc-on-connected", hostSet);
|
|
411
412
|
this._executeSwcScript("swc-on-after-connected", hostSet);
|
|
412
413
|
this.__swc_connected = true;
|
|
414
|
+
const appHostss = import_Utils.SwcUtils.findAllParentAppHostsDirect(this);
|
|
413
415
|
if (appHost && typeof appHost._connected === "function") {
|
|
414
416
|
appHost._connected(this);
|
|
415
417
|
} else if (appHost && typeof appHost) {
|