Skip to content

Do we have something like a collection type (not array type) or serialize model #1867

Answered by EmilTholin
nadeemkhedr asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @nadeemkhedr!

If the big turnoff for the second approach you outline in your question is the changed JSON output, you could add a custom snapshotProcessor to have separate models for your views and actions, but have the JSON output be just arrays.

Example

import { getSnapshot, types } from "mobx-state-tree";

const modelToArrProcessor = {
  // from snapshot to instance
  preProcessor(snapshot) {
    return {
      arr: snapshot
    };
  },
  // from instance to snapshot
  postProcessor(instance) {
    return instance.arr;
  }
};

const Type1 = types.model({
  id: types.identifier
});

const ArrayType1Collection = types.snapshotProcessor(
  types
    .model({
      arr: types.array(Type1)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nadeemkhedr
Comment options

Answer selected by nadeemkhedr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants