xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
554 stars 25 forks source link

Accessing @model decorator arg at runtime #305

Closed justin-hackin closed 2 years ago

justin-hackin commented 3 years ago

For an MST model, I was able to get a static property name as the first parameter supplied to types.model. I believe I can access the arg of @model decorator with instance.$modelType. If this is correct, I think adding a note to https://mobx-keystone.js.org/public/api/index.html#model name property would be helpful. "Unique name for the model type." could be "Unique name for the model type, accessible on the model instance as $modelType property". If that is suiting, I'd be happy to make a PR along with some other docs typos I found.

justin-hackin commented 3 years ago

After reading the following from DataModel docs "Data models, like class models, define the behaviors (actions/views) that can be performed over data, but without tainting the data itself with $modelType / $modelId", I'm not sure if the DataModel lacks a $modelName property or if it's just the snaphot that is not "tainted" with it. It would be unfortunate if I had to change a DataModel to a Model just in order to access $modelName

xaviergonz commented 3 years ago

For an MST model, I was able to get a static property name as the first parameter supplied to types.model. I believe I can access the arg of @model decorator with instance.$modelType

Right now you can do that if it is a class model, yes

If it is a DataModel you cannot do that (data models are thin wrappers over an object), but you could always do

const MyModelTypeName = 'whatever'
@model(MyModelTypeName)
class MyModelType ... {}
....

is there any specific reason why you need to access the model type of a data model?

xaviergonz commented 2 years ago

Closing due to inactivity