Replies: 2 comments
-
Either inheritance (https://mobx-keystone.js.org/classModels#inheritance) or reusing props/functions (more functional). My personal favorite is usually the second though. const sharedProps = {
x: prop<number>(),
y: prop<number>()
} as const
function sharedLength(coords: number[]) {
let len = 0;
for (const c of coords) {
len += c * c;
}
return Math.sqrt(len);
}
@model(...)
class Point2d extends Model({
...sharedProps
}) {
get length() {
return sharedLength(this.x, this.y)
}
}
@model(...)
class Point3d extends Model({
...sharedProps,
z: prop<number>()
}) {
get length() {
return sharedLength(this.x, this.y, this.z)
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi, @xaviergonz , I hope you don't mind me following up on this issue: The only problem I'm facing is how to bind the views as getters- I'm writing the shared views generically (using I'm also having a hard time using the Here's a sandbox example of models A, B and C, and my attempt at implementing this. |
Beta Was this translation helpful? Give feedback.
-
What approach should I take with shareable functionality?
In mst I used
compose
, but I didn't find anything about composition in keystone's docs.p.s. thanks for the library, mst with TS is a really big pain to use :)
Beta Was this translation helpful? Give feedback.
All reactions