YMapComplexEntity
ComplexEntity is an entity that can contain child components, but the methods for adding them to the subtree and removing them from it are internal.
The ymaps3 module has the abstract YMapComplexEntity class inherited from YMapEntity, so it inherits all of its properties and methods. It also has additional methods for working with its own subtree. You can create a Complex Entity component using inheritance from the YMapComplexEntity class:
type YMapSomeLayerProps = {
visible: boolean;
source: string;
};
class YMapSomeLayer extends ymaps3.YMapComplexEntity<YMapSomeLayerProps> {
private _dataSource?: YMapTileDataSource;
private _layer?: YMapLayer;
constructor(props: YMapSomeLayerProps) {
super(props);
const {source, visible} = this._props;
// Create child entity instances.
this._dataSource = new ymaps3.YMapTileDataSource({id: source});
this._layer = new ymaps3.YMapLayer({source, type: 'ground'});
// Add child entities to the component's subtree.
this.addChild(this._dataSource);
if (visible) {
this.addChild(this._layer);
}
}
protected _onUpdate(propsDiff: Partial<YMapSomeLayerProps>): void {
if (propsDiff.visible !== undefined) {
// Remove or add an entity depending on the "visible" value.
propsDiff.visible ? this.addChild(this._layer) : this.removeChild(this._layer);
}
}
}
In the example above, we used methods for interacting with own subtree. The addChild method adds an external entity to the subtree. All child entities are stored in the children array, so the method takes a sequence number in place of which the entity will be added to that array as a second optional argument. The removeChild method removes a child entity from the subtree.
To get a list of child elements, use the read-only children array.
The YMapComplexEntity class constructor also has a second optional argument (options), which is an object with the following properties:
children: An array of child entities that will be added to the subtree immediately after the class instance is initialized.container: Iftrue, creates a child proxy container that will contain a subtree of child components. By default, doesn't create a proxy container.
For more information about proxy containers, see the section.