call user function in foreach loop

Viewed 73

i have understand that i need to change the global scope of this, because in the loop this refers to the window object. But if i try to define a variable in my foreach loop via a function its not working and i dont know why although my functio returns the correct value :(

// simple class for xml import 
function io() {
  this.vertexes = [];
  this.getVertexByID = function(id) {
    this.vertexes.forEach(function(entry) {
      if (id == entry.id) {
        // correct element found, displayed and returned
        console.log(entry);
        return entry;
      }
    });
  }

  this.importXML = function(xmlString) {
    cells = this.xmlToJson(xmlString);

    var parent = graph.getDefaultParent();
    var _this = this;
    graph.getModel().beginUpdate();
    try {
      // addEdges           
      cells.XMLInstance.Edges.Relation.forEach(function(entry) {
        // both will be empty but i dont understand why :(
        fromVertex = _this.getVertexByID(entry.fromNode);
        toVertex = _this.getVertexByID(entry.toNode);
        var e1 = graph.insertEdge(parent, null, '', fromVertex, toVertex);
      });
    } finally {
      graph.getModel().endUpdate();
    }
  }
1 Answers
Related