Merge pull request #32 from techtonik/master

Add support for parsing JSON supplied as text
This commit is contained in:
Dennis Hotson 2013-03-12 14:15:47 -07:00
commit 459904b833
3 changed files with 28 additions and 20 deletions

View File

@ -21,9 +21,9 @@ Try to imagine it as a bunch of springs connected to each other.
Demo
----
[demo - basic](http://dhotson.github.com/springy/demo.html)
: [demo - simplified API](http://dhotson.github.com/springy/demo-simple.html)
: [demo - JSON API](http://dhotson.github.com/springy/demo-json.html)
[basic](http://dhotson.github.com/springy/demo.html)
| [simplified API](http://dhotson.github.com/springy/demo-simple.html)
| [JSON API](http://dhotson.github.com/springy/demo-json.html)
Getting Started
@ -59,11 +59,11 @@ Springy 1.2+ also accepts JSON, see
[demo-json.html](http://dhotson.github.com/springy/demo-json.html):
graphJSON = {
"nodes": ['mark', 'higgs', 'other', 'etc'],
"nodes": ["mark", "higgs", "other", "etc"],
"edges": [
['mark', 'higgs'],
['mark', 'etc'],
['mark', 'other']
["mark", "higgs"],
["mark", "etc"],
["mark", "other"]
]
};

View File

@ -3,26 +3,28 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="springy.js"></script>
<script src="springyui.js"></script>
<script>
var graph = new Graph();
graph.loadJSON(
{
var graphJSON = {
"nodes": [
'Amphitryon',
'Alcmene',
'Iphicles',
'Heracles'
"Amphitryon",
"Alcmene",
"Iphicles",
"Heracles"
],
"edges": [
['Amphitryon', 'Alcmene'],
['Alcmene', 'Amphitryon'],
['Amphitryon', 'Iphicles'],
['Amphitryon', 'Heracles']
["Amphitryon", "Alcmene"],
["Alcmene", "Amphitryon"],
["Amphitryon", "Iphicles"],
["Amphitryon", "Heracles"]
]
}
);
};
jQuery(function(){
var graph = new Graph();
graph.loadJSON(graphJSON);
var springy = jQuery('#springydemo').springy({
graph: graph
});
@ -30,5 +32,7 @@ jQuery(function(){
</script>
<canvas id="springydemo" width="640" height="480" />
</body>
</html>

View File

@ -168,6 +168,10 @@ of nodes and edges:
}
**/
// parse if a string is passed (EC5+ browsers)
if (typeof json == 'string' || json instanceof String) {
json = JSON.parse( json );
}
if ('nodes' in json || 'edges' in json) {
this.addNodes.apply(this, json['nodes']);