mobx-keystone
    Preparing search index...

    Function defineModelMixin

    • Defines a model mixin from a ModelProps object.

      ModelData / ModelCreationData on the composed class resolve to the exact prop types.

      Type Parameters

      • MP extends ModelProps

        Model properties type (inferred from props).

      Parameters

      • props: MP

        Model properties object.

      Returns ModelMixin<ModelPropsToTransformedData<MP>, unknown, MP>

      const countableMixin = defineModelMixin({ quantity: tProp(types.number, 0) })
      
    • Defines a model mixin from a ModelProps object, with a requirement on the base model.

      Pass req<Req>() as the second argument to declare that the base model must already contain the Req shape before the mixin is applied.

      Type Parameters

      • Req extends object

        Shape that must exist on the base model instance (specified via req<Req>()).

      • MP extends ModelProps

        Model properties type (inferred from props).

      Parameters

      • props: MP

        Model properties object.

      • requirement: ReqMarker<Req>

        Requirement marker created by req<Req>().

      Returns ModelMixin<ModelPropsToTransformedData<MP>, Req, MP>

      const producerMixin = defineModelMixin(
      { produced: tProp(types.number, 0) },
      req<{ quantity: number }>()
      )
    • Defines a model mixin from a ModelProps object and a builder that can add actions and other methods.

      The props are applied via ExtendedModel first; the resulting pre-extended class is passed to the builder so you can simply extend Base without calling ExtendedModel yourself. ModelData / ModelCreationData resolve to the exact prop types from MP.

      Type Parameters

      Parameters

      Returns ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, unknown, MP>

      const countableMixin = defineModelMixin(
      { quantity: tProp(types.number, 0) },
      (Base) => class Countable extends Base {
      increment() { this.quantity++ }
      }
      )
    • Defines a model mixin from a ModelProps object and a builder, with a requirement on the base model.

      Pass req<Req>() as the second argument to declare that the base model must already contain the Req shape. The Base class received by the builder includes both the prop types and Req.

      Type Parameters

      Parameters

      Returns ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, Req, MP>

      const producerMixin = defineModelMixin(
      { produced: tProp(types.number, 0) },
      req<{ quantity: number }>(),
      (Base) => class Producer extends Base {
      produceTotal() { return this.produced + this.quantity }
      }
      )