2013-08-09 9 views
5

Ho una classe java che restituisce un oggetto JSON e ho bisogno di passare questo oggetto JSON a gojs javascript, ecco l'esempio del mio file javascript gojs (gojs_org_view js)Come passare l'oggetto JSON al file javascript GOJS da Java/Prime Faces

function init() { 
    var g = go.GraphObject.make; // for conciseness in defining templates 
    myDiagram = new go.Diagram("myDiagram"); // must be the ID or reference to div 
    var graygrad = g(go.Brush, go.Brush.Linear, { 0: "rgb(125, 125, 125)", 1: "rgb(86, 86, 86)", 1: "rgb(86, 86, 86)" }); 
    var unassocArray =[{"id":"12" , "name":"Bugs Bunny", "title":"Head Bunny"}, 
{"id":"13" , "name":"Honey Bunny", "title":"Wife Bunny"}, 
{"id":"14" , "name":"Lola Bunny", "title":"Lil' Bunny"}, 
{"id":"15" , "name":"Mickey Mouse", "title":"Looney In Charge"}]; 

// codice javaScript ...........

function save() { 
    var json = JSON.parse(myDiagram.model.toJson()); 
    document.getElementById("mySavedModel").value = JSON.stringify(json.nodeDataArray, null, ' '); 
    } 
    function load() { 
    var nodeDataArray = 
[ {"id":"1", "pid":"1", "name":"Corrado 'Junior' Soprano", "title":"The Boss", "email":"[email protected]"}, 
    {"id":"2", "pid":"1", "name":"Tony Soprano", "title":"Underboss", "email":"[email protected]"}, 
    {"id":"3", "pid":"1", "name":"Herman 'Hesh' Rabkin", "title":"Advisor", "email":"[email protected]"}, 
    {"id":"4", "pid":"2", "name":"Paulie Walnuts", "title":"Capo", "email":"[email protected]"}, 
    {"id":"5", "pid":"2", "name":"Ralph Cifaretto", "title":"Capo MIA", "email":"[email protected]"}, 
    {"id":"6", "pid":"2", "name":"Silvio Dante", "title":"Consigliere", "email":"[email protected]"}]; 
    var model = new go.TreeModel(); 
    model.nodeKeyProperty = "id"; 
    model.nodeParentKeyProperty = "pid"; 
    model.nodeDataArray = nodeDataArray; 
    myDiagram.model = model; 
    myDiagram.undoManager.isEnabled = true; 
    } 
    function onSelectionChanged(e) { 
    var node = e.diagram.selection.first(); 
    if (node instanceof go.Node) { 
     updateProperties(node.data); 
    } else { 
     updateProperties(null); 
    } 
    } 

    // Update the HTML elements for editing the properties of the currently selected node, if any 
    function updateProperties(data) { 
    if (data === null) { 
     jQuery("#selected").css('visibility', 'hidden'); 
     jQuery("#name").html(""); 
     jQuery("#title").html(""); 
     jQuery("#email").html(""); 
     jQuery("#site").html(""); 
     jQuery("#superior").html(""); 

    } else { 

     jQuery("#selected").css('display', 'block'); 
     jQuery("#name").html("ID: " + data.id + " Name: " + data.fullname || ""); 
     jQuery("#title").html("Title: " + data.title || ""); 
     jQuery("#email").html("Email: " + data.email || ""); 
     jQuery("#site").html("Site: " + data.siteName || ""); 
     jQuery("#superior").html("Superior: " + data.pname || ""); 
    } 
    } 

Le variabili javaScript unassocArray e nodeDataArray sono hard-coded invece questi valori devono venire da java class o xhtml (non sono sicuro quale sia il metodo migliore per inviare oggetti JSON a gojs javascript file sinc Sono nuovo per le prime facce e JSON). Ecco il codice xhtml gojs chiamata

<p:panel id="relationshipsPanel"> 
       <h:outputScript library="js" name="go.js" /> 
       <h:outputScript library="js" name="gojs_org_view.js" /> 
       <h:outputStylesheet library="css" name="gojs_org_view.css"/> 
       <div id="sample"> 
        <div id="myPalette" class="myPaletteClass"></div> 
        <div id="myDiagram" class="myDiagramClass"></div> 
        <div id="myOverview" class="inner"></div> 
        <div id="selected" class="selectedClass"></div> 
       </div> 
      </p:panel> 

Ho provato il valore nascosto, come di seguito, ho aggiunto seguente riga a mio avviso

In gojs_org_view.js ho fatto
window.console.log (" RANA ORG JSON ---------------------> "+ document.getElementById (" relationshipsForm: arrVal "). Value); var nodeDataArray = JSON.parse (document.getElementById ("relationshipsForm: arrVal"). Value);

Nella console sto ottenendo il seguente oggetto JSON dalla vista del javascript, ma il suo errore di lancio "SyntaxError Uncaught: token imprevisto i" a var = nodeDataArray JSON.parse (document.getElementById ("relationshipsForm: arrval") Valore);

risposta

1

È possibile richiedere dati dal server utilizzando una chiamata AJAX e ottenere i dati. se questa non è un'opzione, è possibile inserire i dati in un campo nascosto nella pagina e quindi accedervi nella funzione load(), come si sta facendo nella funzione save() utilizzando document.getElementById.

+0

Ho provato ad usare il valore nascosto Ho messo la riga seguente nella mia vista – RanPaul

+0

prova a convalidare il tuo json usando http://jsonlint.com/ sembra che tu abbia errori nel tuo json – Kishore

+1

grazie @kishore ha funzionato – RanPaul

Problemi correlati