1 | import { ObservableMap, IComputedValue } from "mobx";
|
2 | export interface IViewModel<T> {
|
3 | model: T;
|
4 | reset(): void;
|
5 | submit(): void;
|
6 | isDirty: boolean;
|
7 | changedValues: Map<keyof T, T[keyof T]>;
|
8 | isPropertyDirty(key: keyof T): boolean;
|
9 | resetProperty(key: keyof T): void;
|
10 | }
|
11 | export declare class ViewModel<T> implements IViewModel<T> {
|
12 | model: T;
|
13 | localValues: ObservableMap<keyof T, T[keyof T]>;
|
14 | localComputedValues: ObservableMap<keyof T, IComputedValue<T[keyof T]>>;
|
15 | get isDirty(): boolean;
|
16 | get changedValues(): Map<keyof T, T[keyof T]>;
|
17 | constructor(model: T);
|
18 | isPropertyDirty: (key: keyof T) => boolean;
|
19 | submit(): void;
|
20 | reset(): void;
|
21 | resetProperty(key: keyof T): void;
|
22 | }
|
23 | /**
|
24 | * `createViewModel` takes an object with observable properties (model)
|
25 | * and wraps a viewmodel around it. The viewmodel proxies all enumerable properties of the original model with the following behavior:
|
26 | * - as long as no new value has been assigned to the viewmodel property, the original property will be returned.
|
27 | * - any future change in the model will be visible in the viewmodel as well unless the viewmodel property was dirty at the time of the attempted change.
|
28 | * - once a new value has been assigned to a property of the viewmodel, that value will be returned during a read of that property in the future. However, the original model remain untouched until `submit()` is called.
|
29 | *
|
30 | * The viewmodel exposes the following additional methods, besides all the enumerable properties of the model:
|
31 | * - `submit()`: copies all the values of the viewmodel to the model and resets the state
|
32 | * - `reset()`: resets the state of the viewmodel, abandoning all local modifications
|
33 | * - `resetProperty(propName)`: resets the specified property of the viewmodel
|
34 | * - `isDirty`: observable property indicating if the viewModel contains any modifications
|
35 | * - `isPropertyDirty(propName)`: returns true if the specified property is dirty
|
36 | * - `changedValues`: returns a key / value map with the properties that have been changed in the model so far
|
37 | * - `model`: The original model object for which this viewModel was created
|
38 | *
|
39 | * You may use observable arrays, maps and objects with `createViewModel` but keep in mind to assign fresh instances of those to the viewmodel's properties, otherwise you would end up modifying the properties of the original model.
|
40 | * Note that if you read a non-dirty property, viewmodel only proxies the read to the model. You therefore need to assign a fresh instance not only the first time you make the assignment but also after calling `reset()` or `submit()`.
|
41 | *
|
42 | * @example
|
43 | * class Todo {
|
44 | * \@observable title = "Test"
|
45 | * }
|
46 | *
|
47 | * const model = new Todo()
|
48 | * const viewModel = createViewModel(model);
|
49 | *
|
50 | * autorun(() => console.log(viewModel.model.title, ",", viewModel.title))
|
51 | *
|
52 | * model.title = "Get coffee"
|
53 | *
|
54 | * viewModel.title = "Get tea"
|
55 | *
|
56 | * viewModel.submit()
|
57 | *
|
58 | * viewModel.title = "Get cookie"
|
59 | *
|
60 | * viewModel.reset()
|
61 | *
|
62 | *
|
63 | * @param {T} model
|
64 | * @returns {(T & IViewModel<T>)}
|
65 | * ```
|
66 | */
|
67 | export declare function createViewModel<T>(model: T): T & IViewModel<T>;
|
68 |
|
\ | No newline at end of file |