Skip to content

Commit 544cc85

Browse files
authored
Merge pull request #7 from OpenScriptJs/develop
Develop
2 parents 3ebd32f + b332576 commit 544cc85

2 files changed

Lines changed: 40 additions & 19 deletions

File tree

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<a href="https://www.npmjs.com/package/modular-openscriptjs"><img src="https://img.shields.io/npm/v/modular-openscriptjs.svg?style=flat-square" alt="NPM Version"></a>
1616
<a href="https://github.com/OpenScriptJs/modular-openscript/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/modular-openscriptjs.svg?style=flat-square" alt="License"></a>
1717
<a href="https://github.com/OpenScriptJs/modular-openscript/issues"><img src="https://img.shields.io/github/issues/OpenScriptJs/modular-openscript?style=flat-square" alt="Issues"></a>
18+
<a href="https://www.npmjs.com/package/modular-openscriptjs"><img src="https://img.shields.io/npm/dt/modular-openscriptjs.svg?style=flat-square" alt="Downloads"></a>
1819
</p>
1920

2021
## Introduction
@@ -70,12 +71,14 @@ We didn't just build another framework; we built a toolset for developers who va
7071

7172
### Installation
7273

73-
Start a project
74+
Start a project (All basic configurations are done)
75+
7476
```bash
7577
npm create openscript-app <project-name> <template>
7678
```
7779

7880
Available templates:
81+
7982
- `basic`
8083
- `tailwind`
8184
- `bootstrap`
@@ -261,11 +264,11 @@ configureApp();
261264
262265
### Define Application Events
263266

264-
OpenScript uses a centralized event broker. It's best practice to define all your application events in a single file, typically `events.js` (or `src/events.js`).
267+
OpenScript uses a centralized event broker. It's best practice to define all your application events in a single file, typically `ojs.events.js` (or `src/ojs.events.js`).
265268

266269
If you configured `broker.requireEventsRegistration(true)` in your `ojs.config.js`, only events defined here and registered will be allowed.
267270

268-
Create a `src/events.js` file:
271+
Create a `src/ojs.events.js` file:
269272

270273
```javascript
271274
/**
@@ -448,11 +451,11 @@ You are now set up with the basic structure of an OpenScript application!
448451

449452
OpenScript is built around an **Inversion of Control (IoC) Container**. Instead of importing global instances directly, you access core services via the `app()` helper.
450453

451-
| Service | Access | Description |
452-
| :---------------- | :----------------------- | :--------------------------------------------- |
453-
| **Markup Engine** | `app('h')` | Helper proxy for creating DOM elements. |
454-
| **Router** | `app('router')` | Manages navigation and URL handling. |
455-
| **Broker** | `app('broker')` | Central event bus for decoupled communication. |
454+
| Service | Access | Description |
455+
| :---------------- | :-------------- | :--------------------------------------------- |
456+
| **Markup Engine** | `app('h')` | Helper proxy for creating DOM elements. |
457+
| **Router** | `app('router')` | Manages navigation and URL handling. |
458+
| **Broker** | `app('broker')` | Central event bus for decoupled communication. |
456459

457460
---
458461

@@ -473,6 +476,7 @@ export default class Counter extends Component {
473476
constructor() {
474477
super();
475478
this.count = state(0);
479+
this.count.listener(this); // make this component listen to count state changes
476480
}
477481

478482
// Lifecycle Methods (prefixed with $_)
@@ -579,22 +583,24 @@ Listen to global application events dispatched via the Broker. Methods prefixed
579583
import { parsePayload } from "modular-openscriptjs";
580584
581585
export default class UserProfile extends Component {
582-
// Listen for 'auth' and 'login' event
583-
async $$auth_login(eventData, eventName) {
586+
// Listen for 'auth:login' and 'auth:logout' events
587+
$$auth = {
588+
login_logout: (eventData, eventName) {
584589
// 1. Parse the payload
585-
const data = parsePayload(eventData);
590+
const data = parsePayload(eventData);
586591
587-
console.log("User Logged In:", data.message.get("userId"));
592+
console.log("User Logged In:", data.message.get("userId"));
593+
}
588594
}
589595
}
590596
```
591597
592598
#### 4. Inline Attribute Listeners
593599

594-
For attributes that expect a string script (like `onclick`), use `this.method()`.
600+
For attributes that expect a string script (like `onclick`), use `this.method(methodName, ...arguments)`. This method formats the component methods such that it can be called from the html markup. Example: `onclick="handleClick(arg1, arg2)"`
595601

596602
```javascript
597-
h.button({ onclick: this.method("handleClick") }, "Click");
603+
h.button({ onclick: this.method("handleClick", "arg1", "arg2") }, "Click");
598604
```
599605

600606
---
@@ -715,7 +721,7 @@ h.div(
715721

716722
#### Iteration (`each`)
717723

718-
Iterate over arrays or objects efficiently.
724+
Iterate over arrays or objects efficiently. Try to keep your callbacks stateless to avoid memory leaks.
719725

720726
```javascript
721727
import { each } from "modular-openscriptjs";
@@ -743,8 +749,8 @@ Fragments allow you to group multiple elements without adding an extra node to t
743749
h.$(h.li("Item 1"), h.li("Item 2"));
744750
```
745751

746-
> [!IMPORTANT]
747-
> **Single Root Requirement**: Even when using fragments, your overall component structure or logic block must eventually anchor to a single parent element in the DOM tree.
752+
> [!IMPORTANT]
753+
> **Single Root Requirement**: Even when using fragments, your overall component structure or logic block must eventually anchor to a single parent element in the DOM tree.
748754
> **No Wrapper**: Components returning a fragment are **NOT** wrapped in a custom element (e.g., `<ojs-my-comp>`). This means they cannot easily hold local state or use lifecycle hooks that depend on the wrapper. Use fragments primarily for static content or splitting up render logic.
749755
750756
### Special Attributes
@@ -857,9 +863,24 @@ h.div(
857863
);
858864
```
859865

866+
To style the anonymous component wrapper, you can add a third argument to the `v` helper. The third parameter should be an object like `{ c_attr: {class: 'mb-3 d-block'} }`
867+
868+
```javascript
869+
import { v, app } from "modular-openscriptjs";
870+
const h = app("h");
871+
872+
h.div(
873+
h.h1("Welcome"),
874+
// Only this specific span node updates when 'user' state changes
875+
v(user, (u) => h.span(`Hello, ${u.value.name}!`), {
876+
c_attr: { class: "mb-3 d-block" },
877+
}),
878+
);
879+
```
880+
860881
### Reactivity & Objects
861882

862-
> [!CAUTION]
883+
> [!CAUTION]
863884
> **Object Property Pitfall**: Modifying a property of an object stored in state **does NOT** trigger the state to fire. The state system watches the reference of the value, not the deep properties.
864885
865886
```javascript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-openscriptjs",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "OpenScriptJs Framework - A lightweight, reactive JavaScript framework for building modern web applications",
55
"type": "module",
66
"main": "./dist/modular-openscriptjs.umd.js",

0 commit comments

Comments
 (0)