From 36ff025407314f1b1ecb0ea7359d0fbdbef7117a Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 8 Jun 2015 16:00:13 -0700
Subject: [PATCH] conditional render

---
 js/cells/DMACell.js    |  8 +++-----
 js/lattice/Lattice.js  |  2 +-
 js/three/ThreeModel.js | 12 ++++++++++++
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 06bcd139..28e2757c 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -240,12 +240,9 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
                 if (!this.parts) {
                     this._initParts(function(parts){
                         self.parts = parts;
-                        _.each(this.parts, function(part){
-                            self.addChildren(part.getMesh());
-                        });
                         setVisiblity();
                     });
-                }
+                } else setVisiblity();
                 break;
             case "beam":
 //                if (!this.beams) this.beams = this._initBeams();
@@ -264,7 +261,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
                 if (child.name == "object3D") return;
                 child.visible = visible && (child.name == mode);
             });
-        };
+            three.conditionalRender();
+        }
     };
 
 
diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index 82a8e6db..d170a147 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -86,10 +86,10 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             var index = (new THREE.Vector3()).subVectors(indices, this.get("cellsMin") || indices);
             if (!this.sparseCells[index.x][index.y][index.z]) {
                 var self = this;
+                if (!noRender || noRender === undefined) three.setRenderFlag();
                 this.makeCellForLatticeType(indices, function(cell){
                     self.sparseCells[index.x][index.y][index.z] = cell;
                     self.set("numCells", self.get("numCells")+1);
-                    if (!noRender || noRender === undefined) three.render();
                 });
             } else console.warn("already a cell there");
 
diff --git a/js/three/ThreeModel.js b/js/three/ThreeModel.js
index 6b8d9413..90a2442a 100644
--- a/js/three/ThreeModel.js
+++ b/js/three/ThreeModel.js
@@ -17,6 +17,8 @@ define(['underscore', 'three'], function(_, THREE){
     var animationLoopRunning = false;
     var stopAnimationFlag = false;
 
+    var shouldRender = false;
+
     initialize();
 
     function initialize(){
@@ -122,8 +124,18 @@ define(['underscore', 'three'], function(_, THREE){
         renderer.render(scene, camera);
     }
 
+    function setRenderFlag(){
+        shouldRender = true;
+    }
+
+    function conditionalRender(){
+        if (shouldRender) render();
+    }
+
     return {//return public properties/methods
         render: render,
+        conditionalRender: conditionalRender,
+        setRenderFlag: setRenderFlag,
         startAnimationLoop: startAnimationLoop,
         stopAnimationLoop: stopAnimationLoop,
         sceneRemove: sceneRemove,
-- 
GitLab