anvaka / VivaGraphJS

Graph drawing library for JavaScript
Other
3.73k stars 423 forks source link

Not all nodes are iterated in foreach node #37

Closed antonkulaga closed 11 years ago

antonkulaga commented 11 years ago

When I iterate over nodes I do not get all of them, here is a gist https://gist.github.com/antonkulaga/5532205 showing this problem.

anvaka commented 11 years ago

Vivagraph supports a short-circuiting for nodes iteration. When callback passed into forEachNode() method returns a truthy value, it considers this a sign that the client does not need to continue the iteration. In case of coffeescript the last statement of the lambda expression is actually a return value. So your code is transpiled to something like

this.graph.forEachNode(function(n) {
  var node = {
    id: n.id,
    data: n.data
  };
  return nodes.push(node);
});

Obviously, this is not a falsy value, and vivagraph stops the iteration. Return false from the lambda expression and you get them.

antonkulaga commented 11 years ago

Thank you very much for the answer because without it I would spent a lot of time figuring out what to do. In the same time this is a sign that vivagraph needs some refactoring to become more coffee-script friendly.

anvaka commented 11 years ago

Glad it worked!