mobx-keystone
mobx-keystone copied to clipboard
Custom action metadata via decorators
Hi! First of all thank you for this amazing library, it really is a joy to work with.
We are currently running into a wall when trying to augment the model actions with additional metadata, for example permissions required to execute it. We initially tried simply attaching the metadata to the descriptor, but since mobx-keystone converts these properties to wrappedAction objects internally, this data is lost. We sadly were not able to work around this when experimenting. Since we also do not have access to the model type when setting the decorator, using an external mapping also failed.
We feel like being able to get additional action metadata during runtime would bring a great benefit to this library. All while keeping the clean structure that mobx-keystone provides by also leveraging decorators:
@modelAction
@requirePermissions(['edit'])
setFoo(bar) {
...
}
Is there something we may have missed on how to implement this behavior? Thanks!
As an option - when requirePermissions is called, you can store in global storage it's value with key equal class name + function name.
Hey @quolpr , thanks for your help! This is definitely a good workaround that wouldn't require any changes to the library. I wasn't able to verify yet, but I feel like there are going to be two caveats:
- Models of a project cannot have the same class name
- To work with extended models, figuring out which model an action is from originally would be required
These could easily be solved if we had access to the model id though, since it already puts a "unique" contraint on all models. Do you by any chance know of a way that allows us to get the model id during the initial decorator call?