Simplify addNodes() even more by using variable number of arguments.

This commit is contained in:
anatoly techtonik 2013-02-06 00:22:18 +03:00
parent 90b23f131e
commit 9ac764d634
2 changed files with 9 additions and 8 deletions

View File

@ -5,8 +5,8 @@
<script src="springyui.js"></script>
<script>
var graph = new Graph();
graph.addNodes(['Dennis', 'Michael', 'Jessica', 'Timothy', 'Barbara', 'Franklin'])
graph.addNodes(['Monty', 'James', 'Bianca']);
graph.addNodes('Dennis', 'Michael', 'Jessica', 'Timothy', 'Barbara', 'Franklin')
graph.addNodes('Monty', 'James');
var dennis = graph.newNode({label: 'Dennis'});
var michael = graph.newNode({label: 'Michael'});

View File

@ -69,12 +69,13 @@ Graph.prototype.addNode = function(node) {
return node;
};
Graph.prototype.addNodes = function(list) {
if (typeof(list[0]) == "string") {
list.forEach(function(name) {
var node = new Node(name, data = {label:name});
this.addNode(node);
}, this);
Graph.prototype.addNodes = function() {
// accepts variable number of arguments, where each argument
// is a string that becomes both node identifier and label
for (var i = 0; i < arguments.length; i++) {
var name = arguments[i];
var node = new Node(name, data = {label:name});
this.addNode(node);
}
};