From bcb4019e3613583d2775439d2e9ca0217f7ca59d Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 27 Jan 2015 00:58:10 -0500
Subject: [PATCH] fixed clear all bug

---
 js/fea/dmaCell.js       |  6 ++++++
 js/models/lattice.js    | 14 ++++++++++----
 js/models/threeModel.js |  2 +-
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index 02686857..5cfe4ca7 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -32,6 +32,8 @@
         this.mesh.position.y = position.y;
         this.mesh.position.z = position.z;
 
+        this.mesh.myCell = this;//we need a reference to this instance from the mesh for
+
         this._draw();
 
     //    this.parts = this._createParts(nodes, config);
@@ -60,6 +62,10 @@
     Cell.prototype.rotate = function(rx, ry, rz){
     };
 
+    Cell.prototype._destroy = function(){
+        this.mesh.myCell = null;
+    }
+
 
 
     self.Cell =  Cell;
diff --git a/js/models/lattice.js b/js/models/lattice.js
index 8466fd0f..8b05b5ab 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -22,15 +22,21 @@ Lattice = Backbone.Model.extend({
     },
 
     addCell: function(position){
-        this.get("cells").push(new Cell(position));
-        this.set("numCells", this.get("numCells")+1);
+        var cells = this.get("cells");
+        cells.push(new Cell(position));
+        this.set("numCells", cells.length);
         window.three.render();
     },
 
     removeCell: function(object){
         var cells = this.get("cells");
-        cells.splice(cells.indexOf(object), 1);
-        this.set("numCells", this.get("numCells")-1);
+        var index = cells.indexOf(object.parent.myCell);
+        if (index == -1) {
+            console.warn("problem located cell in cell array");
+            return;
+        }
+        cells.splice(index, 1);
+        this.set("numCells", cells.length);
         window.three.sceneRemove(object);
         window.three.render();
     },
diff --git a/js/models/threeModel.js b/js/models/threeModel.js
index a4ccba9c..0a412d26 100644
--- a/js/models/threeModel.js
+++ b/js/models/threeModel.js
@@ -54,7 +54,7 @@ function ThreeModel(){
 
     function sceneRemove(object){
         var objectToRemove = object;
-        if (object.parent.type != "Scene") {
+        if (object.parent && object.parent.type != "Scene") {
             objectToRemove = object.parent;
         }
         scene.remove(objectToRemove);
-- 
GitLab