You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/components.md
+61-12Lines changed: 61 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,32 +129,81 @@ OpenScript provides conventions for automatically listening to events based on m
129
129
130
130
#### Component Lifecycle & Emitted Events (`$_`)
131
131
132
-
Methods starting with `$_` are treated as listeners for events emitted by the component itself (including lifecycle events).
132
+
Methods starting with `$_` are treated as listeners for events emitted by the component itself.
133
133
134
-
-`$_mounted()`: Called when the component is added to the DOM.
135
-
-`$_rendered()`: Called when the component is rendered.
136
-
-`$_customEvent()`: Listens for `this.emit('customEvent')`.
134
+
> [!WARNING]
135
+
> **Context Safety**: Inside these listeners, **do not rely on `this`** to access the component instance, as the context might not be bound as expected during execution.
136
+
>
137
+
> Instead, use the **`componentId`** passed as the first argument and the **`component(id)`** helper to retrieve the safe instance.
138
+
139
+
-`$_mounted(componentId)`: Called when the component is added to the DOM.
140
+
-`$_rendered(componentId)`: Called when the component is rendered.
141
+
-`$_customEvent(componentId, ...args)`: Listens for `this.emit('customEvent')`.
142
+
143
+
```javascript
144
+
import { component } from"modular-openscriptjs";
145
+
146
+
exportdefaultclassMyComponentextendsComponent {
147
+
$_mounted(componentId) {
148
+
// Correct way to get the instance
149
+
constself=component(componentId);
150
+
self.handleMount();
151
+
}
152
+
}
153
+
```
137
154
138
155
#### Broker Events (`$$`)
139
156
140
157
Methods starting with `$$` are treated as listeners for global events emitted via the **Broker**.
141
158
142
-
-`$$app_started()`: Listens for `app:started` event (dots/colons usually mapped to underscores).
143
-
-`$$user_login()`: Listens for `user:login` event.
159
+
**Signature**: `(eventData, eventName)`
160
+
161
+
-`eventData`: The JSON stringified payload (needs `EventData.parse()`).
162
+
-`eventName`: The string name of the event that triggered this listener.
163
+
164
+
-`$$app_started(eventData, event)`: Listens for `app:started`.
165
+
-`$$user_login(eventData, event)`: Listens for `user:login`.
Context in OpenScript is a mechanism to share state and data across decoupled components and mediators without prop drilling. It acts as a shared object for storing application state.
4
+
5
+
## Concept
6
+
7
+
A **Context** is essentially a shared object (instance of `Context` class) that holds `State` instances or other data. It allows:
8
+
9
+
-**Decoupling**: Mediators can update context, and Components can read/listen to it without knowing about each other.
10
+
-**Shared Data**: accessible via `context('ContextName')`.
11
+
12
+
## Defining & Loading Contexts
13
+
14
+
You do **not** need to define a special class for your context. You simply register it using `putContext`.
// The second argument is a label/path for debugging or loading structure
22
+
putContext(["global"], "AppContext");
23
+
24
+
// Export for usage
25
+
exportconstgc=context("global");
26
+
27
+
// Initialize States in Setup
28
+
exportfunctionsetupContexts() {
29
+
gc.states({
30
+
appName:"My App",
31
+
isLoggedIn:false,
32
+
user:null,
33
+
});
34
+
35
+
// Register in Container (optional but recommended)
36
+
app.value("gc", gc);
37
+
}
38
+
```
39
+
40
+
## Using Context
41
+
42
+
### Accessing Context
43
+
44
+
Use the `context()` helper to retrieve a loaded context.
45
+
46
+
```javascript
47
+
import { context } from"modular-openscriptjs";
48
+
49
+
constgc=context("global");
50
+
```
51
+
52
+
### Bulk State Initialization
53
+
54
+
The `Context` instance has a helper method `states()` to initialize multiple states at once.
55
+
56
+
```javascript
57
+
// Initialize multiple states
58
+
gc.states({
59
+
isLoading:true,
60
+
data: [],
61
+
error:null,
62
+
});
63
+
```
64
+
65
+
## Best Practices
66
+
67
+
### Context vs Component State
68
+
69
+
-**Context**: Use for global data (User session, Theme, Shopping Cart) or data shared between broad sections of the app.
70
+
-**Component State**: Use for local UI behavior (Modal open/close, Form input temporary values).
71
+
72
+
### Handling Large Lists
73
+
74
+
> [!WARNING]
75
+
> **Do not store massive arrays in Context State** if they are only for display (e.g., Infinite Scroll data).
76
+
77
+
For large datasets:
78
+
79
+
1.**Do not put them in a reactive state**.
80
+
2.**Mediators** should handle fetching and posting data.
81
+
3.**Components** should perform "GET" operations to retrieve this data directly (or through a non-reactive service) when needed, rather than listing to a state that holds 1000s of objects.
82
+
4. Use methods like `replaceParent` or specialized logic to append DOM nodes efficiently instead of re-rendering the entire list via state change.
0 commit comments