Autor Zpráva
návštěvník
Profil *
Potřeboval bych poradit jak vyřešit následující situaci:

První funkce je pod objektem Springy.Graph . Tam existuje pole Springy.Graph.nodes na které mohu v klídku aplikovat forEach
    Graph.prototype.createEdges = function(o) {
    var t = this;
    o.colors.forEach(function(e) {
      t.colors.push(e);
      t.fadeColors.push(o.blendingFnc(e,o.fadeColor,o.alphaBlendingRatio));
        }); 
    if (!this.useNodeTree)
      return false;
          
    if (typeof this.topNodes[0]==='undefined')    
      this.nodes.forEach(function(e) {
              if (e.parent_id==null)
          t.topNodes.push(e);
          });
    
    // Go through topnodes and through children
    this.topNodes.forEach(function(e) {
      e.newEdgeForChildren(o);
          });
    };

Problém je ale, že potřebuju aplikovat forEach taky na Springy.Node jenže to nejde:

  Springy.Node.prototype.newEdgeForChildren = function(o)
  {
    var t=this;    
    var chooseColor = function(colors){
      this.counter = typeof this.counter === 'undefined' ?0:this.counter+1;
      if (this.counter==colors.length)
        this.counter = 0;
      if (typeof colors[this.counter]=== 'undefined' )
        throw new Error("Color not defined in createEdge object.");
      if (this.fadeParentNode && typeof colors[this.counter]=== 'undefined' )
        throw new Error("Fade color not defined in createEdge object.");
  
      return {color:graph.colors[this.counter],
              fadeColor:graph.fadeColors[this.counter]};        
    };
    // console.log(this);
    //  throw new Error("hava break");

    // TADY JE PROBLÉM: this je (typ) Springy.Node
    this.forEach(function(e) {
      var id_parent=t.id;
      var id_child=e.id;
            var node1 = graph.nodeSet[id_parent];
            if (node1 == undefined) {
                throw new TypeError("invalid node name: " + id_parent);
            }
            var node2 = graph.nodeSet[id_child];
            if (node2 == undefined) {
                throw new TypeError("2 invalid node name: " + e[1]);
            }
      var colors, fadeColor, weight;
      if (typeof o.colors == 'object')
          colors = chooseColor(o.colors);
            
      graph.newEdge(node1, node2, {color:colors.color, fadeColor:fadeColor, weight: o.max_weight-(e.level>o.max_weight?o.max_weight:o.max_weight-e.level) });
        });
    };

this je typ Springy.Node takže mám chybu:
this.forEach is not a function

Proč? Jak to vyřešit?
návštěvník
Profil *
Už jsem na to přišel, nakonec mi došlo, že forEach je na pole a this je object. Měl jsem tam dát this.children.

Vaše odpověď

Mohlo by se hodit

Neumíte-li správně určit příčinu chyby, vkládejte odkazy na živé ukázky.
Užíváte-li nějakou cizí knihovnu, ukažte odpovídajícím, kde jste ji vzali.

Užitečné odkazy:

Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0