Skip to content

preProcessSnapshot not copied on compose #613

@ssured

Description

@ssured

Sometimes I encode a single behaviour of my objects into a helper function. This helper function then makes it easier to make type definitions more DRY and composable. Below is an example for adding an id identifier strategy.

Right now we have to wrap the type definition call. It would be nice to have a .mixin method, to allow for mixin in these behaviour generating functions.

  // adds an automatic generated unique id to the object
  // and adds `shortId` property for displaying in the ui
  const withIdProperty = type =>
    type
      .props({
        // add id property with random default
        id: t.optional(
          t.identifier(t.string),
          () => `${type.name}-${generateUUID()}`
        ),
      })
      .views(self => ({
        get shortId() {
          // use this in the app
          return self.id.split('-')[1];
        },
      }));

  // current way
  const Record = withIdProperty(
    t.model('Record', {
      name: t.string,
    })
  );

  // proposed way
  const Record = t
    .model('Record', {
      name: t.string,
    })
    .mixin(withIdProperty);

Where .mixin could be implemented like:

type.mixin = function(behaviour) {
  return behaviour(this);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bughas PRA Pull Request to fix the issue is available

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions