1

随着我们对 XPages 版本 9 和 Esri ARcgis javascript api v 3.5 的更新,我们的 dojo 命名空间出现了问题,从而导致了 defineAlreadyDefined 错误。这里列出了一些类似的问题(Using Durandal dojoConfig and ESRI MapsHow can I fix this AMD path conflict?),但即使有了这些帮助,我们也无法使其正常工作。我相信问题是 dojoConfig 语法 - 任何想法或帮助将不胜感激!

这是我们的带有js的xpage源代码的简单版本:

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.resources>
 <xp:styleSheet
  href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dijit/themes/claro/claro.css">
 </xp:styleSheet>
 <xp:styleSheet
  href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri/css/esri.css">
 </xp:styleSheet>
 <xp:script clientSide="true">
 // dojo.registerModulePath("esri","http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri");

 dojoConfig = {
  baseUrl: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri", 
  packages: [
   {
    name: 'dojo',
    location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dojo/"
   },
   {
     name: 'dojox',
     location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dojox"
   },
   { 
     name: 'esri',
     location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri"
    }
   ]};
 </xp:script>
  <xp:script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"
            clientSide="true">
  </xp:script>    
  <xp:dojoModule name="esri.map"></xp:dojoModule>
 </xp:this.resources>
 <xp:eventHandler event="onClientLoad" submit="false">
    <xp:this.script><![CDATA[var map;
    function init(){
     var map = new esri.Map("mapDiv", {
        center: [-56.049, 38.485],
        zoom: 3,
        basemap: "streets"
      });
    }
    dojo.ready(init);

   ]]></xp:this.script>

如果我们包含 dojo.registerModulePath 命令,地图会加载(至少在 FF 中),但会出现错误。没有它,esri dojo 将无法加载 - 它在错误的位置查找 esri 文件。

4

2 回答 2

3

您需要记住几件事,并且可能会相应地更改代码:

  • xPages 已经通过 xsp-config
    文件选项或使用 xPages 参数使用了 dojoconfig。
  • 正如 Per 提到的,dojo 已经在 xPages 中使用,因此您不需要从其他地方加载它(同样适用于 CSS)。

您可以执行以下操作:

  • 选项 1:为地图使用 JS 库的离线副本。您可以将它们作为 JS 资源添加到您的 xPages 应用程序中。您只需在您的 xPage 中指定它们,并像您一样加载 dojo 模块;
  • 选项 2:见下文如何在 xPage 加载之前注入更多 dojoConfig 选项

代码:

<xp:this.properties>
    <xp:parameter name="xsp.client.script.dojo.djConfig" value="packages: exPackages" />
</xp:this.properties>
<xp:this.beforePageLoad>
    <![CDATA[#{javascript:
        var exCon = facesContext.getExternalContext();
        var response = exCon.getResponse();
        var writer = response.getWriter();
        writer.write("<script>\n");
        writer.write("var exPackages=[{name:'esri',location:'http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri'}]\n");
        writer.write("</script>\n");
        }]]>
</xp:this.beforePageLoad>
<xp:this.resources>
    <xp:dojoModule name="esri.map"></xp:dojoModule>
</xp:this.resources>

更新:代码的小修正。

Update2: After briefly checking ArcGis website, it seems they choose to provide Dojo together with their API (which is wrong in my opinion). See https://developers.arcgis.com/en/javascript/jshelp/inside_dojoversion.html (part2), although it will not help you much as they don't provide a feasible solution for Dojo 1.8.x

Seeing that their API is not free, I think the best way would be to contact them, ask for ESRI part of the API as separate download, host it on your own servers and follow either Option 1 or 2. Moreover, the version of API you try to use is based on Dojo 1.8.3, while Domino 9 has Dojo 1.8.

于 2013-07-19T06:31:49.667 回答
0

OK, this was a problem for me too! I've been able to get it working by using the following code:

    <xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.resources>
        <xp:script clientSide="true">
            dojo.registerModulePath("esri","http://js.arcgis.com/3.8/js/esri");
            dojoConfig = { baseUrl: "http://js.arcgis.com/3.8/js/esri",
            packages: [ { name: 'esri', location: "http://js.arcgis.com/3.8/js/esri" } ]};
        </xp:script>
        <xp:styleSheet href="http://js.arcgis.com/3.8/js/esri/css/esri.css" />
        <xp:styleSheet href="http://js.arcgis.com/3.8/js/esri/dijit/css/Popup.css" />
  <xp:dojoModule name="esri.map"></xp:dojoModule>
    </xp:this.resources>
    <xp:eventHandler event="onClientLoad" submit="false">
        <xp:this.script><![CDATA[var map;
    function init(){
     var map = new esri.Map("mapDiv", {
        center: [0,53],
        zoom: 10,
        basemap: "streets"
      });
    }
    dojo.ready(init);

   ]]></xp:this.script>
    </xp:eventHandler>
    <div id="mapDiv" style="width:1000px;height:600px"></div>
    </xp:view>

I think the order of scripts is important and you don't need to include the esri.map script twice.

于 2014-02-14T12:00:14.437 回答