Use "self" to define properties #1831
Unanswered
OfficialDarkComet
asked this question in
Ideas
Replies: 1 comment
-
Thanks for the reply. The result you have in your reply is what I'm wanting, but when I try it through, it returns { first: 4 }. Would it make any difference if 4 was passed when I created it and not frozen/literal value? Or the second property was defined using a function?
function AddOne(value) {
return value + 1;
}
const MyModel = types
.model("MyModel")
.props({
first: types.number
})
.extend((self) => ({
state: { second: AddOne(self.first), third: "apple" }
}));
const myModel = MyModel.create({ first: 4 });
console.log(myModel);
// My result => {first: 4}
On Nov 10, 2021 at 5:21 PM, Emil Tholin ***@***.***> wrote:
Hi @OfficialDarkComet <https://github.com/OfficialDarkComet>!
You could reference first on self by splitting up the model definition into
a props followed by an extend returning an object with a property state:
*Example*
const MyModel = types
.model("MyModel")
.props({
first: 4
})
.extend((self) => ({
state: { second: self.first + 1, third: "apple" }
}));
const myModel = MyModel.create();
console.log(myModel);// {first: 4, second: 5, third: "apple"}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1831 (comment)>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If this exists or there is a work around, please let me know, otherwise I wanted to put the idea out there for MST maintainers to consider adding.
Goal: Get a variable passed to model property prior to creation.
Idea: You are already able to create some pretty dynamic model properties with the use of functions, however, unlike model actions, views, etc... you are unable to do two things when defining properties:
If the first is easier to implement then do that, however my idea is for the second, the ability to reference "self".
I am assuming the the model properties are added sequentially (one after another), if so the ability to reference those properties to use to create properties lower in the list would be awesome!
Example:
const MyModel = types
.model("My Model")
.props({
first: 4
second: self.first + 1
third: "apple"
})
In the example, because the property "first" is declared prior to "second", this allows "second" to reference it.
That's the idea and like I said, if I have missed anything and this is possible already then let me know. Otherwise, feedback is welcome. Thank you for your time and thank you for those who have built, maintained and added to MST.
- Logan King (aka DarkComet)
Beta Was this translation helpful? Give feedback.
All reactions