mavoweb / treecle

WIP. A toolbox for hierarchical JS objects.
https://treecle.mavo.io
MIT License
6 stars 0 forks source link

Config option for how to get parents #30

Open LeaVerou opened 5 months ago

LeaVerou commented 5 months ago

There are many tree implementations that are already doing their own parent pointers. For example, in DOM nodes, you can always get to the parent via node.parentNode. We should have a config option for this so we don't end up doing duplicate work and introducing unnecessary error conditions.

This could be a config option (which also becomes a Treecle instance property). For treecle to properly use this, it needs to know both how to read parents, and set parents.

Strawman: A parent property whose value is either a string (read/write property name) or a { get: function(node), set: function(node, otherNode) } for more elaborate schemas.

For example, for the DOM, this could look like this:

parent: {
    get: n => n.parentNode,
    set: (node, newParent) => newParent.append(node);
}

I'm not convinced this is a good design: what if we need to specify where exactly the node gets appended? Not to mention this affects other properties too.

Perhaps it should be possible to just say parent: "parentNode" and just error if we try to use any properties that require writing parents. Or even have an option to turn that off and fail silently (since the tree may automatically handle that).

People should be able to use treecle for read-only stuff like walk() or closest() or getting complex familial relationships without having to also use its parent mgmt system (or have to implement complex parent setting helpers they don't need just to provide a value to parent.set).