-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decorate - compose decorators for a single prop #1652
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,24 @@ decorate(Todo, { | |
}) | ||
``` | ||
|
||
For applying multiple decorators on a single property, you can pass an array of decorators. | ||
```javascript | ||
import { decorate, observable } from "mobx" | ||
import { serializable, primitive } from "serializr" | ||
import persist from 'mobx-persist'; | ||
|
||
class Todo { | ||
id = Math.random(); | ||
title = ""; | ||
finished = false; | ||
} | ||
decorate(Todo, { | ||
title: [serializable(primitive), persist('object'), observable], | ||
finished: [serializable(primitive), observable] | ||
}) | ||
``` | ||
Note: Composing decorators can sometimes lead to strange things when one decorator modifies the [property descriptor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) in a way that is unexpected for the other decorators. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not so sure if my explanation is clear enough. Would love to get a feedback about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: not all decorators can be composed together, and this functionality is just best-effort. Some decorators affect the instance directly and can 'hide' the effect of other decorators that only change the prototype |
||
|
||
### Computed values | ||
|
||
<i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/javascript-derive-computed-values-and-manage-side-effects-with-mobx-reactions">Egghead.io lesson 3: computed values</a></i> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mension what the application order is. I assume serializable is applied first, then persist and finally observable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct