-
Notifications
You must be signed in to change notification settings - Fork 641
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
getParentOfType(node, parentType) #477
Comments
Sounds useful Interested in attempting a PR? `mst-operations.ts` should be
a fine place to add it to
Op do 19 okt. 2017 om 20:36 schreef Javier Gonzalez <
[email protected]>:
… Will try to get the first parent found of a given node that matches a
given type, or undefined if none found.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#477>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhM1od-BN-ohOFHn1Bb8-mE6LmJIOks5st5adgaJpZM4P_rB->
.
|
Right now I'm using the following code: export function getParentOfType<S, M>(target: IStateTreeNode, type: IType<S, M>): M | undefined {
let currentNode = target;
while (hasParent(currentNode)) {
currentNode = getParent(currentNode);
if (getType(currentNode) === type) {
return currentNode as M;
}
}
return undefined;
} But I have a few doubts. What does getParent do? the docs say:
but then the actual method does: so, should getParent be changed so it returns null? should its docs be changed? should the new method return null if not found? undefined? throw? |
Just hit this issue with something like const Model = types.model('Model', {
name: types.string,
})
.views((self) => ({
get builder() {
// return getParent(self); - this returns the `models` Map
return getParent(getParent(self)); // - returns the Builder model
}
));
const Builder = types.model('Builder', {
models: types.map(Model)
}); Indeed the Map is the first parent but not exactly what I've expected. |
The code above works. I'm just waiting for @mweststrate or @mattiamanzati to get me a hint on the desired semantics to adapt it and make a pull request |
@xaviergonz sorry for late response! PR is welcome, I would go for the two function api version ( |
so then get method will throw if there's no such parent right? |
yep
Op ma 15 jan. 2018 om 19:22 schreef Javier Gonzalez <
[email protected]>:
… so then get method will throw if there's no such parent right?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#477 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhNHZYCc7sb6KDWg54sC4Y9PBlcGwks5tK5dhgaJpZM4P_rB->
.
|
@mweststrate but this approach leads to harder tests implementations since in my tests files I always have to pass the top store of the entire tree, else it will break test run. I followed the bookshop implementation:
is there any workaround to prevent throwing error? |
hasParent?
Op za 10 mrt. 2018 16:19 schreef Pavel Poberezhnyi <[email protected]
…:
@mweststrate <https://github.com/mweststrate> but this approach leads to
harder tests implementations since in my tests files I always have to pass
the top store of the entire tree, else it will break test run. I followed
the bookshop implementation:
.views(self => ({
get appStore() {
return getParent(self);
},
}))
is there any workaround to prevent throwing error?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#477 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhIdSGXAAInRcGRxAo_KV1X2N5pvMks5tc-8bgaJpZM4P_rB->
.
|
oh, I didn't see this method. Thanks and sorry for this |
on the todo list now. |
PR has been merged to master now, so will be released soon |
Will try to get the first parent found of a given node that matches a given type, or undefined if none found.
Basically I'm using getParent with different depths to achieve the same effect at the moment, but I'm starting to loose track :) (plus that implementation would be more resiliant to model changes)
The text was updated successfully, but these errors were encountered: