Giter Site home page Giter Site logo

Comments (10)

xaviergonz avatar xaviergonz commented on May 18, 2024 1

Nevermind, ModelData<M> and ModelCreationData<M> are public, I just never wrote about them in the docs

from mobx-keystone.

xaviergonz avatar xaviergonz commented on May 18, 2024 1

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.

xaviergonz avatar xaviergonz commented on May 18, 2024

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:

  1. 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)
  1. 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),
})
  1. 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.

xaviergonz avatar xaviergonz commented on May 18, 2024

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.

terrysahaidak avatar terrysahaidak commented on May 18, 2024

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.

xaviergonz avatar xaviergonz commented on May 18, 2024

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.

terrysahaidak avatar terrysahaidak commented on May 18, 2024

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.

xaviergonz avatar xaviergonz commented on May 18, 2024

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.

terrysahaidak avatar terrysahaidak commented on May 18, 2024

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.

xaviergonz avatar xaviergonz commented on May 18, 2024

Closing, I think this issue has been open long enough :)

from mobx-keystone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.