Skip to content

ViewModelStore interface

Interface represeting a store for managing ViewModels

OPTIONAL USE

This is not required for targeted usage of this package, but can be helpful for accessing ViewModels from everywhere by ViewModelLookup

Reference to source code

Method and properties

getIds(vmLookup)

Retrieves ids ViewModels based on vmLookup.

Example

ts
vmStore.getIds(MyVM) // ["id"]
vmStore.getIds(ViewComponentOfMyVM) // ["id"]

getId(vmLookup)

Retrieves the id of the last ViewModel based on vmLookup.

Example

ts
vmStore.getId(MyVM) // "id"
vmStore.getId(ViewComponentOfMyVM) // "id"

mountedViewsCount

The total number of views that are currently mounted.

has(vmLookup)

Checks whether a ViewModel instance exists in the store.
Requires vmLookup.

get(vmLookup)

Retrieves the last ViewModel instance from the store based on vmLookup.

TIP

If you node more than one VM use getAll(vmLookup) method

Example

ts
import { mobx-view-model } from "mobx-view-model";

class UserSelectVM extends ViewModelBase {
  selectedUser = {
    id: '1',
    name: 'John Doe'
  }
}

vmStore.get(UserSelectVM)?.selectedUser.id; // '1'

getAll(vmLookup)

Retrieves all ViewModel instances from the store based on vmLookup.

attach(viewModel)

Attaches a ViewModel to the store.

dettach(viewModelId)

Detaches a ViewModel from the store using its ID.

isAbleToRenderView(viewModelId)

Determines if a ViewModel is able to render based on its ID.

createViewModel(config)

Creates a new ViewModel instance based on the provided configuration.

Example:

ts
import {
  ViewModelStoreBase,
  ViewModel,
  ViewModelCreateConfig,
} from 'mobx-view-model';

export class ViewModelStoreImpl extends ViewModelStoreBase {
  createViewModel<VM extends ViewModel<any, ViewModel<any, any>>>(
    config: ViewModelCreateConfig<VM>,
  ): VM {
    const VM = config.VM;
    return new VM(config);
  }
}

processCreateConfig(config)

Process the configuration for creating a ViewModel.
This method is called just before creating a new ViewModel instance.
It's useful for initializing the configuration, like linking components to the ViewModel class.

linkComponents()

Link React components with ViewModel class.

unlinkComponents()

Unlink React components with ViewModel class.

generateViewModelId(config)

Generates a unique ID for a ViewModel based on the provided configuration.

clean()

Clean up resources associated with the ViewModel store.
Clean all inner data structures.

Released under the MIT License.