Closed aaronsamuel137 closed 9 years ago
Added comment for insertChild call. Reassign to @aaronsamuel137 for verification.
Looks good. Closing.
This pattern occurs in a few places in this sim. We discussed this at our developer meeting on 4/2/2015. We'd prefer a different pattern instead of using the pattern:
this.insertChild( 3, energyGraphNode );
It would be better to create a layer for the node like so:
this.energyGraphLayer = new Node();
this.addChild( this.energyGraphLayer );
and then later:
this.energyGraphLayer.addChild( this.energyGraphNode );
This way adding new nodes won't be as likely to break things in the future if the ordering of the children changes.
Added using of layer pattern instead of insertChild. Reassign to @aaronsamuel137 for verification
Looking great, thanks!
There are two other places where insertChild is being used that seem off to me.
This code:
// add necessary pendulum sliders
pendulumLabModel.numberOfPendulumsProperty.link( function( numberOfPendulums ) {
var numberDifference = currentNumberOfSliders - numberOfPendulums;
// remove extra sliders
if ( numberDifference > 0 ) {
for ( ; numberDifference--; ) {
content.removeChildWithIndex( pendulumSlidersNodeStorage[ currentNumberOfSliders - numberDifference - 1 ], currentNumberOfSliders - numberDifference - 1 );
currentNumberOfSliders--;
}
}
// add necessary sliders
else if ( numberDifference < 0 ) {
for ( ; numberDifference++; ) {
content.insertChild( currentNumberOfSliders - numberDifference, pendulumSlidersNodeStorage[ currentNumberOfSliders - numberDifference ] );
currentNumberOfSliders++;
}
}
} );
Could be simplified to this (or something similar):
content.addChild( pendulumSlidersNodeStorage[0] );
// add necessary pendulum sliders
pendulumLabModel.numberOfPendulumsProperty.link( function( numberOfPendulums ) {
if ( numberOfPendulums === 1 && content.children.length === 2 ) {
content.removeChildWithIndex( pendulumSlidersNodeStorage[ 1 ], 1 );
}
else if ( numberOfPendulums === 2 && content.children.length === 1 ) {
content.addChild( pendulumSlidersNodeStorage[ 1 ] );
}
} );
Do you have any thoughts or objections about simplifying this code?
Feel free to comment on this @jonathanolson if you have another opinion.
Assigning to @andrey-zelenkov.
Fixed insertChild for protractorNode. Layer pattern used instead of insertChild for PendulumSlidersNode. I implemented simplification suggested by @aaronsamuel137 with minor fixes. Thanks! Reassign to @aaronsamuel137 for verification.
Looks good to me. Thanks! Closing
During code review #56 I noticed this in ProtractorNode:
Most PhET code uses addChild() instead. Can you either document why this is necessary or use addChild? Thanks.