5

我有一个返回 JSON 对象的 java 类,我需要将此 JSON 对象传递给 gojs javascript,这是我的 gojs javascript 文件的示例(gojs_org_view.js)

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"}];

//一些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":"boss@sopranos.net"},
    {"id":"2", "pid":"1", "name":"Tony Soprano", "title":"Underboss", "email":"underboss@sopranos.net"},
    {"id":"3", "pid":"1", "name":"Herman 'Hesh' Rabkin", "title":"Advisor", "email":"hesh@sopranos.net"},
    {"id":"4", "pid":"2", "name":"Paulie Walnuts", "title":"Capo", "email":"paulie@sopranos.net"},
    {"id":"5", "pid":"2", "name":"Ralph Cifaretto", "title":"Capo MIA", "email":"ralph@sopranos.net"},
    {"id":"6", "pid":"2", "name":"Silvio Dante", "title":"Consigliere", "email":"silvio@sopranos.net"}];
    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 || "");
    }
  }

javascript 变量 unassocArray 和 nodeDataArray 是硬编码的,而不是这些值必须来自 java 类或 xhtml(我不确定哪种是将 JSON 对象发送到 gojs javascript 文件的最佳方法,因为我不熟悉主要面孔和 JSON)。这是调用gojs的xhtml代码

<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>

我尝试了如下隐藏值,我在视图中添加了以下行

在 gojs_org_view.js 我做了
window.console.log("RANA ORG JSON--------------------->"+document.getElementById("relationshipsForm:arrVal") 。价值); var nodeDataArray = JSON.parse(document.getElementById("relationshipsForm:arrVal").value);

在控制台中,我从视图中获取以下 JSON 对象到 javascript,但它在 var nodeDataArray = JSON.parse(document.getElementById("relationshipsForm:arrVal").value) 处抛出错误“Uncaught SyntaxError: Unexpected token i”;

4

1 回答 1

1

您可以使用 AJAX 调用从服务器请求数据并获取数据。如果这不是一个选项,那么您可以将数据放在页面上的隐藏字段中,然后在您的load()函数中访问它,就像您在save()函数中使用document.getElementById.

于 2013-08-12T15:02:48.950 回答