-
-
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 2 commits
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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ | |
"rollup": "^0.41.6", | ||
"rollup-plugin-filesize": "^1.3.2", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"serializr": "^1.3.0", | ||
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. I added 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. N.P. |
||
"size-limit": "^0.2.0", | ||
"tape": "^4.2.2", | ||
"ts-jest": "^22.0.0", | ||
|
@@ -116,4 +117,4 @@ | |
"<rootDir>/node_modules/" | ||
] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5225,6 +5225,10 @@ [email protected]: | |
range-parser "~1.2.0" | ||
statuses "~1.3.1" | ||
|
||
serializr@^1.3.0: | ||
version "1.3.0" | ||
resolved "https://registry.yarnpkg.com/serializr/-/serializr-1.3.0.tgz#6c7f977461d54a24bb1f17a03ed0ce61d239b010" | ||
|
||
[email protected]: | ||
version "1.12.3" | ||
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.12.3.tgz#9f4ba19e2f3030c547f8af99107838ec38d5b1e2" | ||
|
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