rasti 4.0.1 → 4.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -5
- package/dist/rasti.js +7 -3
- package/dist/rasti.js.map +1 -1
- package/dist/rasti.min.js +1 -1
- package/dist/rasti.min.js.map +1 -1
- package/es/Component.js +1 -1
- package/es/Component.js.map +1 -1
- package/es/Model.js +3 -1
- package/es/Model.js.map +1 -1
- package/es/View.js +3 -1
- package/es/View.js.map +1 -1
- package/lib/Component.cjs +1 -1
- package/lib/Component.cjs.map +1 -1
- package/lib/Model.cjs +3 -1
- package/lib/Model.cjs.map +1 -1
- package/lib/View.cjs +3 -1
- package/lib/View.cjs.map +1 -1
- package/package.json +36 -26
- package/src/Component.js +1 -1
- package/src/Model.js +3 -1
- package/src/View.js +3 -1
- package/types/Component.d.ts +238 -0
- package/types/Emitter.d.ts +111 -0
- package/types/Model.d.ts +123 -0
- package/types/View.d.ts +173 -0
- package/types/index.d.ts +15 -0
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<picture>
|
|
3
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.0.
|
|
4
|
-
<img alt="Rasti.js" src="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.0.
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.1.0-alpha.0/docs/logo-dark.svg">
|
|
4
|
+
<img alt="Rasti.js" src="https://cdn.jsdelivr.net/gh/8tentaculos/rasti@v4.1.0-alpha.0/docs/logo.svg" height="120">
|
|
5
5
|
</picture>
|
|
6
6
|
</p>
|
|
7
7
|
|
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
It provides declarative, composable **components** for building state-driven UIs.
|
|
14
14
|
Its low-level MVC core, inspired by **Backbone.js**’s architecture, provides **models**, **views** and **event emitters** as the fundamental building blocks.
|
|
15
15
|
|
|
16
|
-
[](https://github.com/8tentaculos/rasti/actions/workflows/ci.yml)
|
|
17
17
|
[](https://www.npmjs.com/package/rasti)
|
|
18
18
|
[](https://unpkg.com/rasti/dist/rasti.min.js)
|
|
19
19
|
[](https://www.npmjs.com/package/rasti)
|
|
20
20
|
[](https://www.jsdelivr.com/package/npm/rasti)
|
|
21
|
+
[](https://github.com/8tentaculos/rasti/blob/master/LICENSE)
|
|
21
22
|
|
|
22
23
|
## Key Features
|
|
23
24
|
|
|
@@ -35,6 +36,8 @@ Its low-level MVC core, inspired by **Backbone.js**’s architecture, provides *
|
|
|
35
36
|
Seamlessly integrates into existing **Backbone.js** legacy projects.
|
|
36
37
|
- **Standards-Based** 📐
|
|
37
38
|
Built on modern web standards, no tooling required.
|
|
39
|
+
- **TypeScript Support** 🧩
|
|
40
|
+
Ships with type definitions for strict typing of models, views, components, props, and events.
|
|
38
41
|
|
|
39
42
|
## Getting Started
|
|
40
43
|
|
|
@@ -179,7 +182,7 @@ Counter.mount({ model }, document.body);
|
|
|
179
182
|
- **Lightweight and Efficient**
|
|
180
183
|
Minimal footprint with optimized performance, ensuring smooth updates.
|
|
181
184
|
- **Just the Right Abstraction**
|
|
182
|
-
Keeps you close to the DOM with no over-engineering. Fully hackable
|
|
185
|
+
Keeps you close to the DOM with no over-engineering. Fully hackable — if you're curious about how something works, just check the source code.
|
|
183
186
|
|
|
184
187
|
## Example
|
|
185
188
|
|
|
@@ -189,6 +192,88 @@ You can find a sample **TODO application** in the [example folder](https://githu
|
|
|
189
192
|
|
|
190
193
|
For detailed information on how to use **Rasti**, refer to the [API documentation](/docs/api.md).
|
|
191
194
|
|
|
195
|
+
## TypeScript
|
|
196
|
+
|
|
197
|
+
**Rasti** ships with TypeScript declarations out of the box. The types are bundled in the package and resolved automatically.
|
|
198
|
+
|
|
199
|
+
### Components
|
|
200
|
+
|
|
201
|
+
Pass generics explicitly to type the resulting class:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
const Header = Component.create<{ handleAddTodo: (title: string) => void }>`
|
|
205
|
+
<header>...</header>
|
|
206
|
+
`;
|
|
207
|
+
|
|
208
|
+
new Header({ handleAddTodo: (t) => console.log(t) }); // ✅
|
|
209
|
+
|
|
210
|
+
// With a typed model:
|
|
211
|
+
const App = Component.create<{}, any, AppModel>`<main>...</main>`;
|
|
212
|
+
App.mount({ model: new AppModel() }, document.body);
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Without generics, `Component.create` stays permissive (parity with JS):
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
const Plain = Component.create`<div></div>`;
|
|
219
|
+
new Plain({ anything: 'goes' }); // ✅
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Models
|
|
223
|
+
|
|
224
|
+
Type the attributes with `Model<Attrs>`. Use **declaration merging** to surface the auto-generated getters/setters as instance properties:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
import { Model } from 'rasti';
|
|
228
|
+
|
|
229
|
+
interface TodoAttrs { title: string; completed: boolean; }
|
|
230
|
+
|
|
231
|
+
class Todo extends Model<TodoAttrs> {
|
|
232
|
+
preinitialize() {
|
|
233
|
+
this.defaults = { title: '', completed: false };
|
|
234
|
+
}
|
|
235
|
+
toggle() { this.completed = !this.completed; }
|
|
236
|
+
}
|
|
237
|
+
interface Todo extends TodoAttrs {} // Exposes this.title, this.completed
|
|
238
|
+
|
|
239
|
+
const t = new Todo({ title: 'x' });
|
|
240
|
+
t.title.toUpperCase(); // ✅
|
|
241
|
+
t.on('change:completed', (m, value) => value && /* boolean */ console.log('done'));
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Helper types
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import {
|
|
248
|
+
EventHandler,
|
|
249
|
+
RenderExpression,
|
|
250
|
+
Attrs,
|
|
251
|
+
Props,
|
|
252
|
+
State,
|
|
253
|
+
ComponentModel,
|
|
254
|
+
} from 'rasti';
|
|
255
|
+
|
|
256
|
+
// Typed event handler with `this` bound to the component
|
|
257
|
+
const onClick: EventHandler<Counter, MouseEvent> = function(ev) {
|
|
258
|
+
this.props.label;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// Typed render expression (`(component) => any`)
|
|
262
|
+
const renderLabel: RenderExpression<Counter> = ({ props }) => props.label;
|
|
263
|
+
|
|
264
|
+
// Extract types from existing classes
|
|
265
|
+
type T = Attrs<Todo>; // TodoAttrs
|
|
266
|
+
type P = Props<Counter>; // CounterProps
|
|
267
|
+
type S = State<Counter>; // CounterState
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Known limitations
|
|
271
|
+
|
|
272
|
+
- **Template interpolation callbacks are `any`**. Functions inside `Component.create\`...\`` template literals (`${({ model }) => ...}`, `onClick=${function() { this.x }}`) cannot be inferred from the surrounding template. To type them, annotate explicitly: `function(this: MyComponent, ev) { ... }` or `({ model }: MyComponent) => ...`.
|
|
273
|
+
- **`Model<A>` instance keys require declaration merging**. TypeScript can't add `A`'s keys to a `class extends Model<A>` automatically — see the `interface Todo extends TodoAttrs {}` pattern above.
|
|
274
|
+
- **`this.$()` can return `null`**. It mirrors `querySelector`, so handle the empty case (`?.`) and pass a type argument to narrow the element: `this.$<HTMLInputElement>('input.edit')?.focus()`. `this.$$()` returns a `NodeListOf<HTMLElement>` (also narrowable).
|
|
275
|
+
- **`this.model` / `this.state` are optional**. Both are `undefined` unless provided, so guard (`this.model?.foo`) or assert (`this.model!`) when you know one was passed. Both accept a Rasti `Model` or a model from another library (e.g. Backbone); Components subscribe to `change` events automatically when the object exposes `on`/`off`.
|
|
276
|
+
|
|
192
277
|
## Working with LLMs
|
|
193
278
|
|
|
194
279
|
For those working with LLMs, there is an [AI Agents reference guide](/docs/AGENTS.md) that provides API patterns, lifecycle methods, and best practices, optimized for LLM context. You can share this guide with AI assistants to help them understand **Rasti**'s architecture and component APIs.
|
|
@@ -209,4 +294,3 @@ We strive to minimize breaking changes between major versions. However, if you'r
|
|
|
209
294
|
## Contributing
|
|
210
295
|
|
|
211
296
|
Contributions are welcome! Share feature ideas or report bugs on our [GitHub Issues page](https://github.com/8tentaculos/rasti/issues).
|
|
212
|
-
|
package/dist/rasti.js
CHANGED
|
@@ -419,7 +419,6 @@
|
|
|
419
419
|
* @param {...*} [args] Additional arguments passed to `preinitialize` and `parse` methods
|
|
420
420
|
* @property {object|Function} defaults Default attributes for the model. If a function, it's called bound to the model instance to get defaults.
|
|
421
421
|
* @property {object} previous Object containing previous attributes when a change occurs.
|
|
422
|
-
* @property {string} attributePrefix Static property that defines a prefix for generated getters/setters. Defaults to empty string.
|
|
423
422
|
* @example
|
|
424
423
|
* import { Model } from 'rasti';
|
|
425
424
|
*
|
|
@@ -733,6 +732,9 @@
|
|
|
733
732
|
* Static property that defines a prefix for generated getters/setters.
|
|
734
733
|
* When set, all attribute properties will be prefixed (e.g., 'attr_name' instead of 'name').
|
|
735
734
|
* Useful for avoiding naming conflicts or creating a consistent property naming convention.
|
|
735
|
+
* @static
|
|
736
|
+
* @memberof module:Model
|
|
737
|
+
* @name attributePrefix
|
|
736
738
|
* @type {string}
|
|
737
739
|
* @default ''
|
|
738
740
|
* @example
|
|
@@ -778,7 +780,7 @@
|
|
|
778
780
|
* @property {object|Function} events Object in the format `{'event selector' : 'listener'}`. It will be used to bind delegated event listeners to the root element. If it is a function, it will be called to get the events object, bound to the view instance. See {@link module_view_delegateevents View.delegateEvents}.
|
|
779
781
|
* @property {object} model A model or any object containing data and business logic.
|
|
780
782
|
* @property {Function} template A function that returns a string with the view's inner HTML. See {@link module_view__render View.render}.
|
|
781
|
-
* @property {
|
|
783
|
+
* @property {string} uid Unique identifier for the view instance. This can be used to generate unique IDs for elements within the view. It is automatically generated and should not be set manually.
|
|
782
784
|
* @example
|
|
783
785
|
* import { View, Model } from 'rasti';
|
|
784
786
|
*
|
|
@@ -1163,6 +1165,8 @@
|
|
|
1163
1165
|
* For server-side rendering, this counter should be reset to `0` on every request to ensure that the generated
|
|
1164
1166
|
* unique IDs match those on the client, enabling seamless hydration of components.
|
|
1165
1167
|
* @static
|
|
1168
|
+
* @memberof module:View
|
|
1169
|
+
* @name uid
|
|
1166
1170
|
* @type {number}
|
|
1167
1171
|
* @default 0
|
|
1168
1172
|
*/
|
|
@@ -2803,7 +2807,7 @@
|
|
|
2803
2807
|
// In this case, where the component updates, it handles children recycling.
|
|
2804
2808
|
const addChild = (component) => {
|
|
2805
2809
|
let out = component;
|
|
2806
|
-
let found
|
|
2810
|
+
let found;
|
|
2807
2811
|
// Check if child already exists by key.
|
|
2808
2812
|
if (component.key) {
|
|
2809
2813
|
found = previousChildren.find(prev => prev.key === component.key);
|