mvc-kit 2.11.0 → 2.11.1
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.
|
@@ -495,6 +495,18 @@ class CartViewModel extends ViewModel<CartState> {
|
|
|
495
495
|
const [state, vm] = useSingleton(CartViewModel);
|
|
496
496
|
```
|
|
497
497
|
|
|
498
|
+
**Convenience hooks** (optional): For singletons used in many components, co-export a hook from the ViewModel file:
|
|
499
|
+
```typescript
|
|
500
|
+
// viewmodels/AuthViewModel.ts
|
|
501
|
+
export class AuthViewModel extends ViewModel<AuthState> { ... }
|
|
502
|
+
export const useAuth = () => useSingleton(AuthViewModel);
|
|
503
|
+
|
|
504
|
+
// components/Header.tsx — single import
|
|
505
|
+
import { useAuth } from '../viewmodels/AuthViewModel';
|
|
506
|
+
const [state, vm] = useAuth();
|
|
507
|
+
```
|
|
508
|
+
Use sparingly (2–3 app-wide singletons). The class stays framework-agnostic.
|
|
509
|
+
|
|
498
510
|
**Pattern C — Shared Collection** (different views of same data):
|
|
499
511
|
```tsx
|
|
500
512
|
// UsersTable uses UsersViewModel → subscribes to UsersCollection
|
|
@@ -213,7 +213,7 @@ class UsersListVM extends ViewModel<FilterState> {
|
|
|
213
213
|
## Sharing Patterns
|
|
214
214
|
|
|
215
215
|
1. **Pattern A** (default): Parent ViewModel passes props to presentational children
|
|
216
|
-
2. **Pattern B**: Singleton ViewModel via `useSingleton` for app-wide state (define `static DEFAULT_STATE` for arg-free calls)
|
|
216
|
+
2. **Pattern B**: Singleton ViewModel via `useSingleton` for app-wide state (define `static DEFAULT_STATE` for arg-free calls). For singletons used in many components, optionally co-export a convenience hook: `export const useAuth = () => useSingleton(AuthViewModel);` -- keeps the class framework-agnostic while reducing imports at call sites. Use sparingly (2--3 app-wide singletons).
|
|
217
217
|
3. **Pattern C**: Separate ViewModels sharing a singleton Collection
|
|
218
218
|
|
|
219
219
|
## ViewModel Events
|
|
@@ -213,7 +213,7 @@ class UsersListVM extends ViewModel<FilterState> {
|
|
|
213
213
|
## Sharing Patterns
|
|
214
214
|
|
|
215
215
|
1. **Pattern A** (default): Parent ViewModel passes props to presentational children
|
|
216
|
-
2. **Pattern B**: Singleton ViewModel via `useSingleton` for app-wide state (define `static DEFAULT_STATE` for arg-free calls)
|
|
216
|
+
2. **Pattern B**: Singleton ViewModel via `useSingleton` for app-wide state (define `static DEFAULT_STATE` for arg-free calls). For singletons used in many components, optionally co-export a convenience hook: `export const useAuth = () => useSingleton(AuthViewModel);` — keeps the class framework-agnostic while reducing imports at call sites. Use sparingly (2–3 app-wide singletons).
|
|
217
217
|
3. **Pattern C**: Separate ViewModels sharing a singleton Collection
|
|
218
218
|
|
|
219
219
|
## ViewModel Events
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mvc-kit",
|
|
3
|
-
"version": "2.11.
|
|
3
|
+
"version": "2.11.1",
|
|
4
4
|
"description": "Zero-magic, class-based reactive ViewModel library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/mvc-kit.cjs",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"dev:fullapp": "vite --config examples/react/FullApp/vite.config.ts",
|
|
12
12
|
"dev:complexapp": "vite --config examples/react/ComplexApp/vite.config.ts",
|
|
13
13
|
"dev:workerapp": "vite --config examples/react/WorkerApp/vite.config.ts",
|
|
14
|
+
"dev:authexample": "vite --config examples/react/AuthExample/vite.config.ts",
|
|
14
15
|
"build": "rm -rf dist && tsc -p tsconfig.build.json && vite build",
|
|
15
16
|
"postinstall": "node agent-config/bin/postinstall.mjs",
|
|
16
17
|
"test": "vitest run",
|