Comments (10)
Nevermind, ModelData<M>
and ModelCreationData<M>
are public, I just never wrote about them in the docs
from mobx-keystone.
Just wrote them :) But I think it is nice to keep this one open in case somebody else has the same issue.
from mobx-keystone.
That's because $modelType
is actually required to be in model snapshots before fromSnapshot
can be used on them (or else you will get a plain object and not a model instance).
There are several ways around this:
- Inject the model type in the snapshot (you also need to inject it for any submodels you may have)
const backendData = ...
const snapshot = {
...modelSnapshotInWithMetadata(TodoModel, backendData),
// if there are submodels
submodel1: modelSnapshotInWithMetadata(SubModel, backendData.submodel1)
}
const todo = fromSnapshot<TodoModel>(snapshot)
- Create a todo instance (again, you might have to call new Model for sub-models)
const backendData = ...
const todo = new TodoModel({
...backendData,
// if there are submodels
submodel1: new Submodel(backendData.submodel1),
})
- Actually make the backend inject the "$modelType" in returned data models (the easiest one if it is possible)
I was thinking of adding a registrable callback so whenever fromSnapshot finds an object without $modelType
will be called with the object and can return the model class it should be, but that'd mean you'd need to somehow be able to infer it from the structure...
Something like:
const disposer = registerModelResolver((obj: any) => {
if (somehowIknowThatObjStructureIsLikeFooModel) {
return FooModel
}
else {
return undefined
}
})
But I'm not sure how useful that might be.
The only way (AFAIK) to avoid that would be to force the whole type system to be runtime type checked, and use this type check info to infer the model type (which is what mobx-state-tree does).
from mobx-keystone.
Another option would be to have a subTypeResolver per model, so you would be able to use the path instead of the object structure.
@model(...)
class FooRootModel extends Model(...) {
}
registerSubTypeResolver(FooRootModel, (path, subObj) => {
if (path.is(["a", "b"]) return FooSubModel1
// if FooSubModel1 has unknown submodels it will try to use its subtype resolver to figure them out
if (path.is(["a", "objMap", any]) return FooSubModel2
if (path.is(["a", "whatever", (k) => k.startsWith("bleh"), "something"]) return FooSubModel3
// just a plain object, array or primitive, do the default
return undefined
})
const m = fromSnapshot(FooRootModel, backendData)
with probably a default implementation where if it is declared with "tcProp" it will try to use that first to figure out the model type
Maybe that's more useful?
from mobx-keystone.
Don't you find it like just a really simple and common case which requires a lot of extra steps just like I do?
I think there should be some kind of type which just allows you to pass some JSON into the model constructor, just like that:
// this is just a pseudocode
// TodoModel.Data represents a real type of all the data we are going to pass
// just the same type we would use in the constructor
const json: TodoModel.Data = {
text: 'test',
}
const m = new TodoModel(json);
Even without submodules or so. My point here is that I'm just want to create a new instance. Want to use not the exact snapshot, but something I would just write in the object that constructor accepts. Maybe I'm not familiar with TypeScript enough so I'm seeing it different, just from my perspective, but please let me know where I'm wrong.
By the way, any
works fine, it lets me pass it, but this isn't cool :)
from mobx-keystone.
If the model doesn't have sub-models then it should work.
e..g
@model("asd")
class MM extends Model({text: prop<string>()}) {}
const data = {text: "hi"}
const mm = new MM(data)
const data2: ModelCreationData<MM> = {text: "hi"}
const mm2 = new MM(data2)
(reminds me I should make ModelCreationData
public I guess :) )
from mobx-keystone.
ModelCreationData
– is the exact type I wanted, thank you.
Is the MM.Data
as a replacement of ModelCreationData<MM>
make any sense for you? Not sure if it's even possible or has some perf impact.
from mobx-keystone.
You would need to write typeof MM.CreationData
then. I've always found that people get confused on why it doesn't work when they don't write typeof
.
from mobx-keystone.
Got it, so ok, when it looks fine.
For some reason, ModelCreationData
didn't work for me when I've tried it (just been guessing by its name), but seems like a problem with VSCode, sometimes it just lags.
Seems like my question is answered and that issue is resolved, thank you!
Should I keep it open until you add some docs for ModelCreationData type?
from mobx-keystone.
Closing, I think this issue has been open long enough :)
from mobx-keystone.
Related Issues (20)
- Stream utilities on top of keystone HOT 3
- How to provide default/fixed value for inheriting class? HOT 4
- Mental model and idea behind keystone's tweaks/patches related to mobx observe
- Root Registry / Backrefs with deep observation HOT 5
- Keystone types supporting `onActionMiddleware` HOT 3
- Is it valid to start actions in onActionMiddleware's onStart/onFinish? HOT 2
- How to listen "initial" onPatches on RootStore?
- incorrect detaches from root store when applying snapshot HOT 4
- model does not detached from root store if it in array HOT 4
- Exception when detaching model that have active reaction with reference HOT 4
- Not able to access static methods of parent class from child class when inheriting with generic types. HOT 1
- Does it support Typescript 5.X features? HOT 3
- Unnecessary loops causes low performance, how to optimize the performance HOT 2
- Binary data in models HOT 3
- getRootStore returns undefined. HOT 5
- yjs Map not recommended for key-value data. HOT 4
- Thank you HOT 2
- WeakMap key must be an Object HOT 5
- Getting 'extendObservable' only accepts plain objects as second argument HOT 1
- onInit does not execute from static creation method HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobx-keystone.