Skip to content

Commit

Permalink
Update documentation and copyright.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Jan 28, 2018
1 parent 55e6f4d commit 21094e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 BusFaster Ltd
Copyright (c) 2017-2018 BusFaster Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TodoCode extends shim(TodoData) {

}

const Todo = mst(TodoCode, TodoData);
const Todo = mst(TodoCode, TodoData, 'Todo');
```

ES6 methods become views (assumed to have no side-effects) unless decorated
Expand All @@ -67,9 +67,13 @@ a block of code with methods (views and actions) to use with the type.

The `mst` function binds the two together (producing a new type "inheriting"
`TodoData`), and the `TodoCode` class should not be used directly.
A third, optional parameter gives the resulting model a name.
Names are required for polymorphism to work correctly, when serializing
models to JSON containing fields supporting different possible subclasses.

The `shim` function is a tiny wrapper that makes TypeScript accept MST types
as superclasses.
as superclasses. It must be used in the `extends` clause of the ES6 class
defining the views and actions.

The major differences compared to ordinary ES6 classes are:

Expand Down Expand Up @@ -112,13 +116,53 @@ class SpecialTodoCode extends shim(SpecialTodoData, Todo) {

}

const SpecialTodo = mst(SpecialTodoCode, SpecialTodoData);
const SpecialTodo = mst(SpecialTodoCode, SpecialTodoData, 'SpecialTodo');
```

If adding new properties to the superclass, it's important to pass the
unmodified superclass as the second parameter to `shim` so that
`super` is initialized correctly.

Polymorphism
------------

Instances of subclasses can be used in place of their parent classes inside models.
Due to `mobx-state-tree` implementation internals, both classes must have been defined
before the first parent class instance has been created anywhere in the program.

Snapshots containing polymorphic types require type names in the serialized JSON,
to identify the correct subclass when applying the snapshot.
A special key `$` is automatically added in snapshots when an object in the tree
belongs to a subclass of the class actually defined in the model.

The default key `$` for types can be changed by passing a different string to the
`setTypeTag` function before creating any model instances. For example:

```TypeScript
import { getSnapshot } from 'mobx-state-tree';
import { setTypeTag } from 'classy-mst';

setTypeTag('type');

const Store = types.model({
todos: types.array(Todo)
});

const store = Store.create({
todos: [
SpecialTodo.create({ title: 'Baz' })
]
});

console.log(getSnapshot(store));
```

The above prints:

```
{ todos: [ { title: 'Baz', done: false, count: 0, type: 'SpecialTodo' } ] }
```

Volatile state
--------------

Expand Down Expand Up @@ -273,4 +317,4 @@ License

[The MIT License](https://raw.githubusercontent.com/charto/classy-mst/master/LICENSE)

Copyright (c) 2017 BusFaster Ltd
Copyright (c) 2017-2018 BusFaster Ltd
2 changes: 1 addition & 1 deletion src/classy-mst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is part of classy-mst, copyright (c) 2017 BusFaster Ltd.
// This file is part of classy-mst, copyright (c) 2017-2018 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

import { IType, IModelType, IStateTreeNode, types } from 'mobx-state-tree';
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is part of classy-mst, copyright (c) 2017 BusFaster Ltd.
// This file is part of classy-mst, copyright (c) 2017-2018 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

export { mst, shim, action, setTypeTag, ModelInterface } from './classy-mst';

0 comments on commit 21094e1

Please sign in to comment.