|
| 1 | +import type Alpine from 'alpinejs'; |
| 2 | + |
1 | 3 | import { |
2 | 4 | AlpineComponents, |
3 | 5 | makeAlpineConstructor, |
4 | | - type Globals |
| 6 | + Globals |
5 | 7 | } from '@nxtlvlsoftware/alpine-typescript'; |
6 | 8 |
|
7 | 9 | import { |
@@ -35,23 +37,29 @@ export namespace MyComponents { |
35 | 37 |
|
36 | 38 | export function bootstrap ( |
37 | 39 | options: Partial<Options> = defaultOptions, |
38 | | - alpinejs: Globals.Alpine = window.Alpine |
| 40 | + alpinejs: typeof Alpine = window.Alpine |
39 | 41 | ): void { |
40 | 42 | const opts: Options = { |
41 | 43 | ...defaultOptions, |
42 | 44 | ...options |
43 | 45 | }; |
44 | 46 |
|
45 | | - // make typescript happy |
46 | | - let alpine = <Globals.AlpineWithComponents>alpinejs; |
47 | | - |
48 | 47 | document.addEventListener('alpine:init', () => { |
49 | 48 | // Register any alpine stores your components rely on here. |
50 | 49 | // You can pull them and any other dependencies in as default params |
51 | 50 | // for a custom component constructor when alpine-components:init is fired. |
52 | 51 | }); |
53 | 52 |
|
54 | 53 | document.addEventListener('alpine-components:init', () => { |
| 54 | + // make typescript happy |
| 55 | + let alpine = Globals.castToAlpineWithComponents(alpinejs); |
| 56 | + if (alpine === null) { |
| 57 | + if (opts.logErrors) { |
| 58 | + console.error('Alpine object does not have Components properties injected. Did the Components package boot properly?'); |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + |
55 | 63 | // Basic registration by falling back to the prototype name when left undefined. |
56 | 64 | // Can be used in your HTML with: x-data="ToggleComponent([true|false])" |
57 | 65 | alpine.Components.register(AlertComponent); |
@@ -90,7 +98,7 @@ export namespace MyComponents { |
90 | 98 | // Allow booting alpine and the components package with a single call to our |
91 | 99 | // bootstrap function. This makes the client script a bit simpler. |
92 | 100 | if (opts.bootstrapComponents) { |
93 | | - AlpineComponents.bootstrap(opts, alpine); |
| 101 | + AlpineComponents.bootstrap(opts, alpinejs); |
94 | 102 | } |
95 | 103 | } |
96 | 104 |
|
|
0 commit comments