Trying to understand the inner workings of the codebase: where is "position" defined?
See original GitHub issueI was looking at the example codepen shared here https://codesandbox.io/s/baklavajs-example-jyc6f?file=/src/App.vue:2032-2040
And I see a method that takes in coordinates along with a Node type:
methods: {
addNodeWithCoordinates(nodeType, x, y) {
const n = new nodeType();
this.editor.addNode(n);
n.position.x = x;
n.position.y = y;
return n;
}
}
I also see that node instances have positions defined when looking at nodes in the web dev tools. However, when I do a search where Nodes are defined (node.d.ts) I do not see “position”. I also cannot find “position” when I search all my libraries related to baklavajs.
Does anyone know where position comes from? I think learning about this will help me understand a lot more about inheritance, perhaps.
The reason I ask originally came from me trying Typescript for fun, but having trouble defining types properly in order to effectively use it. For example, in my Editor.vue I have
getPosition(node) {
return [node.position.x, node.position.y]
},
but when I try to define node like
getPosition(node: Node) {
console.log(node)
return [node.position.x, node.position.y]
},
TypeScript tells me that Node does not have “position”. I saw that too by looking into node.d.ts. But… then how does it exist?
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
@newcat Oooh!! Thank you so much!
Using BaklavaJS is teaching me much more than any course I’ve taken 😉 The freedom to be able to build my own node generators, make my own Node classes, etc feels like I’m playing a video game.
Edit: does this mean I need to cast the type node:IViewNode?
Your
nodeType
would benodeType: new () => Node
in this case.Then I’d just use the
IViewNode
for setting the view-related properties like this:This separation between interfaces and actual implementations has historical reasons and won’t exist in Baklava V2 anymore, but for now we have to live with it unfortunately.
Edit: You might be able to cast n to something like this:
but I can’t test it right now