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
---- ----
[demo - basic](http://dhotson.github.com/springy/demo.html) [basic](http://dhotson.github.com/springy/demo.html)
: [demo - simplified API](http://dhotson.github.com/springy/demo-simple.html) | [simplified API](http://dhotson.github.com/springy/demo-simple.html)
: [demo - JSON API](http://dhotson.github.com/springy/demo-json.html) | [JSON API](http://dhotson.github.com/springy/demo-json.html)
Getting Started Getting Started
@ -59,11 +59,11 @@ Springy 1.2+ also accepts JSON, see
[demo-json.html](http://dhotson.github.com/springy/demo-json.html): [demo-json.html](http://dhotson.github.com/springy/demo-json.html):
graphJSON = { graphJSON = {
"nodes": ['mark', 'higgs', 'other', 'etc'], "nodes": ["mark", "higgs", "other", "etc"],
"edges": [ "edges": [
['mark', 'higgs'], ["mark", "higgs"],
['mark', 'etc'], ["mark", "etc"],
['mark', 'other'] ["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="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="springy.js"></script> <script src="springy.js"></script>
<script src="springyui.js"></script> <script src="springyui.js"></script>
<script> <script>
var graph = new Graph(); var graphJSON = {
graph.loadJSON(
{
"nodes": [ "nodes": [
'Amphitryon', "Amphitryon",
'Alcmene', "Alcmene",
'Iphicles', "Iphicles",
'Heracles' "Heracles"
], ],
"edges": [ "edges": [
['Amphitryon', 'Alcmene'], ["Amphitryon", "Alcmene"],
['Alcmene', 'Amphitryon'], ["Alcmene", "Amphitryon"],
['Amphitryon', 'Iphicles'], ["Amphitryon", "Iphicles"],
['Amphitryon', 'Heracles'] ["Amphitryon", "Heracles"]
] ]
} };
);
jQuery(function(){ jQuery(function(){
var graph = new Graph();
graph.loadJSON(graphJSON);
var springy = jQuery('#springydemo').springy({ var springy = jQuery('#springydemo').springy({
graph: graph graph: graph
}); });
@ -30,5 +32,7 @@ jQuery(function(){
</script> </script>
<canvas id="springydemo" width="640" height="480" /> <canvas id="springydemo" width="640" height="480" />
</body> </body>
</html> </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) { if ('nodes' in json || 'edges' in json) {
this.addNodes.apply(this, json['nodes']); this.addNodes.apply(this, json['nodes']);