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/osm.md
+123-7Lines changed: 123 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,18 +24,81 @@ The `h` object is a **Proxy**. When you access a property on it (e.g., `h.div`,
24
24
25
25
## Attributes & Properties
26
26
27
-
You can pass attributes as the first argument to the tag function if it is an object (and not a DOM node or State).
27
+
Attributes can be passed as **objects** at any point in the argument list. OpenScript treats any argument that is an object (and not a DOM Node or State) as an attributes object.
28
28
29
29
```javascript
30
-
h.a(
30
+
// Attributes can be anywhere
31
+
h.div("Text first", { id:"my-div" }, " More text");
32
+
33
+
// Multiple attributes objects are merged
34
+
// Note: Underscores in keys are converted to dashes (data_role -> data-role)
Special attributes like `class` are intelligently handled. If you pass multiple class attributes (in different objects), they are **concatenated** rather than overwritten.
> **Avoid `addEventListener`**: Do not use the standard `element.addEventListener` methods on nodes created by OpenScript. Doing so can lead to memory leaks because the framework cannot track and automatically remove these listeners when the component is unmounted.
51
+
52
+
Instead, always use the `listeners` attribute object. OpenScript modifies the DOM nodes to include safe `addListener` and `removeListener` methods that integrate with the framework's lifecycle management.
53
+
54
+
```javascript
55
+
h.button(
56
+
{
57
+
listeners: {
58
+
click: (e) =>console.log("Safe click", e),
59
+
},
60
+
},
61
+
"Safe Button",
62
+
);
63
+
```
64
+
65
+
### Extending Node Functionality (`methods`)
66
+
67
+
You can attach custom methods to a DOM node at runtime using the `methods` attribute. This is useful for exposing logic that needs to be called externally (e.g., after retrieving the node via `document.getElementById`).
68
+
69
+
```javascript
70
+
h.div(
31
71
{
32
-
href:"https://example.com",
33
-
class:"link primary",
34
-
target:"_blank",
35
-
id:"main-link",
72
+
id:"my-widget",
73
+
methods: {
74
+
refresh:function () {
75
+
this.innerHTML="Refreshed!"; // 'this' refers to the DOM element
76
+
},
77
+
getData: () => ({ id:1, value:"test" }),
78
+
},
36
79
},
37
-
"Visit Example",
80
+
"Widget Content",
38
81
);
82
+
83
+
// Usage elsewhere:
84
+
constwidget=document.getElementById("my-widget");
85
+
if (widget) {
86
+
widget.methods().refresh();
87
+
}
88
+
```
89
+
90
+
### Inline Function Calls (`h.func`)
91
+
92
+
The `h.func` helper formats a function and its arguments as a string, suitable for placement in inline event attributes (like `onclick`). This is how you pass function calls into string-based attributes.
93
+
94
+
```javascript
95
+
// In a component
96
+
render() {
97
+
returnh.div({
98
+
// Generates: onclick="greet('Levi', 42)"
99
+
onclick:h.func("greet", "Levi", 42)
100
+
}, "Click to Greet")
101
+
}
39
102
```
40
103
41
104
### Boolean Attributes
@@ -74,3 +137,56 @@ Strings and numbers are automatically converted to text nodes.
74
137
```javascript
75
138
h.p("You have ", 5, " notifications.");
76
139
```
140
+
141
+
## Logic & Helpers
142
+
143
+
OpenScript provides helper functions to handle logic like iterations and conditionals directly within your markup structure.
144
+
145
+
### Executing Logic (`h.call`)
146
+
147
+
If you need to run arbitrary logic during the creation of the node structure, you can use `h.call(callback)`. The callback should return a valid OSM node (string, element, or array).
0 commit comments