@@ -8,36 +8,11 @@ This way can be helpful when:
88
99Follow the steps:
1010
11- 1 . Make your own ViewModelStore implementation
12-
13- ``` ts file="view-model.store.impl.ts"
14- import {
15- ViewModelParams ,
16- ViewModelStoreBase ,
17- ViewModel ,
18- ViewModelCreateConfig ,
19- } from ' mobx-view-model' ;
20-
21- export class ViewModelStoreImpl extends ViewModelStoreBase {
22- constructor (protected rootStore : RootStore ) {
23- super ();
24- }
25-
26- createViewModel<VM extends ViewModel <any , ViewModel <any , any >>>(
27- config : ViewModelCreateConfig <VM >,
28- ): VM {
29- const VM = config .VM ;
30- // here is you sending rootStore as
31- // first argument into VM (your view model implementation)
32- return new VM (this .rootStore , config );
33- }
34- }
35- ```
36-
37- 2 . Make your own ` ViewModel ` implementation with sharing ` RootStore `
11+ 1 . Make your own ` ViewModel ` implementation with accepting ` RootStore ` as ` constructor ` parameter
3812
3913``` ts
4014// view-model.ts
15+ // interface for your view model
4116import { ViewModel as ViewModelBase } from ' mobx-view-model' ;
4217
4318export interface ViewModel <
@@ -46,7 +21,9 @@ export interface ViewModel<
4621> extends ViewModelBase <Payload , ParentViewModel > {}
4722```
4823
49- ``` ts
24+ ``` ts{15}
25+ // view-model.impl.ts
26+ // implementation for your interface
5027import { ViewModelBase, ViewModelParams } from 'mobx-view-model';
5128
5229import { ViewModel } from './view-model';
@@ -65,26 +42,57 @@ export class ViewModelImpl<
6542 super(params);
6643 }
6744
45+ // example of your custom methods
46+ // and properties
6847 get queryParams() {
6948 return this.rootStore.router.queryParams.data;
7049 }
50+ }
51+
52+ ```
7153
72- protected getParentViewModel(
73- parentViewModelId : Maybe <string >,
74- ): ParentViewModel | null {
75- return this .rootStore .viewModels .get <ParentViewModel >(parentViewModelId );
54+
55+ 1 . Make your own ` ViewModelStore ` implementation with accepting ` RootStore ` as ` constructor ` parameter and overriding ` createViewModel ` method for transfer ` rootStore `
56+
57+ ``` ts{8,11,22,23,24}
58+ // view-model.store.impl.ts
59+ import {
60+ ViewModelParams,
61+ ViewModelStoreBase,
62+ ViewModel,
63+ ViewModelCreateConfig,
64+ } from 'mobx-view-model';
65+ import { ViewModelImpl } from "./view-model.impl.ts"
66+
67+ export class ViewModelStoreImpl extends ViewModelStoreBase {
68+ constructor(protected rootStore: RootStore) {
69+ super();
7670 }
77- }
7871
72+ createViewModel<VM extends ViewModel<any, ViewModel<any, any>>>(
73+ config: ViewModelCreateConfig<VM>,
74+ ): VM {
75+ const VM = config.VM;
76+
77+ // here is you sending rootStore as
78+ // first argument into VM (your view model implementation)
79+ if (ViewModelImpl.isPrototypeOf(VM)) {
80+ return new VM(this.rootStore, config);
81+ }
82+
83+ // otherwise it will be default behaviour
84+ // of this method
85+ return super.createViewModel(config);
86+ }
87+ }
7988```
8089
81903 . Add ` ViewModelStore ` into your ` RootStore `
8291
83- ``` ts
92+ ``` ts{8}
8493import { ViewModelStore } from 'mobx-view-model';
8594import { ViewModelStoreImpl } from '@/shared/lib/mobx';
8695
87-
8896export class RootStoreImpl implements RootStore {
8997 viewModels: ViewModelStore;
9098
@@ -94,10 +102,9 @@ export class RootStoreImpl implements RootStore {
94102}
95103```
96104
97-
981054 . Create ` View ` with ` ViewModel `
99106
100- ``` tsx
107+ ``` tsx{2,4,10}
101108import { ViewModelProps, withViewModel } from 'mobx-view-model';
102109import { ViewModelImpl } from '@/shared/lib/mobx';
103110
0 commit comments