Scripting Containment Relationships
The BOM editor allows you to say that instances of one class are contained within instances of another. For example, if there were classes for Car and Widget, instances of the Widget class can be said to be contained by a Car instance. For the contained relationship, the contained objects are affected by the container’s lifecycle events. So when the container (Car in our example) is destroyed, all the contained (Widget in our example) objects will be destroyed too.
The diagram below shows that Widget objects can be contained by a Car or a Bike. However, for an individual Widget object instance, it can only belong to a Car or a Bike instance, not both at once.
There are two ways to model this in the BOM editor. First, a Composition link can be drawn between the two classes, like this:
Alternatively, the Car and Bike class can be given an attribute called widgets of type Widget, as shown below:
Both of these two Car/Widget relationships appear the same when scripting.
There is an attribute of the Car object called widgets, which will be an Array, that can contain Widget objects. This would be processed in a similar way to the Array processing examples already covered.
For example, to create a Car and add two Widgets, we can write:
var car = factory.com_example_data_containment_createCar(); data.car.model = "Saloon"; var widget = factory.com_example_data_containment_createWidget(); data.widget.description = "M8 Bolt"; data.car.widgets.push(widget); widget = factory.com_example_data_containment_createWidget (); data.widget.description = "M8 Nut"; car.widgets.push(widget); |