Skip to content

Commit ea7c770

Browse files
committed
Level 3: document deferred initialization semantics
1 parent ede8db8 commit ea7c770

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

‎packages/docs/src/en/advanced/extending.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ window.Alpine = Alpine
6161
window.Alpine.start()
6262
```
6363

64+
<a name="deferring-initialization"></a>
65+
### Deferring initialization
66+
67+
An extension that needs to load something before Alpine initializes a tree can defer that tree from an initialization interceptor:
68+
69+
```js
70+
Alpine.interceptInit((el) => {
71+
if (! el.hasAttribute('x-needs-library')) return
72+
73+
Alpine.deferInit(el, import('./library.js'))
74+
})
75+
```
76+
77+
`Alpine.deferInit()` must be called synchronously from the interceptor. Alpine will leave the element and its descendants uninitialized until every deferred promise settles, then continue from the next interceptor without re-running earlier ones. Rejected promises are passed to Alpine's error handler before initialization continues.
78+
79+
Other trees continue initializing normally, and the global `alpine:initialized` event remains synchronous: it may fire before a deferred tree has resumed.
80+
6481
Now that we know where to use these extension APIs, let's look more closely at how to use each one:
6582

6683
<a name="custom-directives"></a>

0 commit comments

Comments
 (0)