ArcGIS API for JavaScript压缩版(compact)与标准版的区别

前言

在我们下载下来的API文件中可以看到里面包含了两个版本,一个是常用的标准版,另一个是压缩版。

compact

如果到各自文件夹里面对比看的话,除了两个版本的init.js文件大小不一样外(压缩版小点,900多kb),其他文件大小一致。除了init.js文件大小不同的区别外,还有以下两个区别。

区别一

The compact build removes the dependency on the dijit namespace upon initial download, meaning that if you don’t need the dojo dijits they won’t be loaded. A side-effect of this is that a new info window and slider are provided.

压缩版把最开始dijit下面的一些依赖给移除掉了,这里的dijit指的是dojo框架自身的一些微件(控件),ArcGIS在dijit之上封装了一些常用微件如信息窗(infowindow)缩放条(slider)等。下面通过最简单的例子看一看。

标准版

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="https://js.arcgis.com/3.28/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
    <script src="https://js.arcgis.com/3.28/"></script>
    <script>
      var map;

      require(["esri/map", "dojo/domReady!"], function(Map) {
        map = new Map("map", {
          basemap: "topo",  //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });
        new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_Currents_World/GPServer/MessageInABottle");
        console.log("标准版,esri.tasks.QueryTask直接就可以用,因为在init.js里面提前就有了");
      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>

标准版

压缩版

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="https://js.arcgis.com/3.28compact/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
    <script src="https://js.arcgis.com/3.28compact/"></script>
    <script>
      var map;

      require(["esri/map", "dojo/domReady!"], function(Map) {
        map = new Map("map", {
          basemap: "topo",  //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });
        new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_Currents_World/GPServer/MessageInABottle");
        console.log("压缩版,esri.tasks.QueryTask也可以用,不过是init.js里面某行代码require触发的,而不是直接把QueryTask类的代码拷贝了一份放到了init.js");
      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>

压缩版

可以很明显的看出来,init.js文件大小不一样,而且同一个加载地图的功能,压缩版明显加载的js文件数更多。就是因为压缩版为了减小init.js的体积,把里面的一些依赖给移除掉了。

区别二

The compact build includes less modules than the standard build. If your application requires objects from modules not included in the compact build you will need to load them using require(). For example, if you want to perform geoprocessing with the compact build you will need to add the following require() statement to your application.
require([“esri/tasks/Geoprocessor”, … ], function(Geoprocessor, … ){ … });
These two features reduce the size of the build significantly. Less JavaScript code to execute means less work the browser has to do.

这个说的是比起标准版,压缩版里面包含了更少的模块。比如说esri/tasks/Geoprocessor,这个在压缩版中是没有事先引入的,你必须通过require手动引入。其实这条跟没说差不多,因为我并不知道哪些是事先已经引入的,用的时候为了保险我只能先手动require进来。

参考链接

https://developers.arcgis.com/javascript/3/jshelp/intro_accessapi.html#compact-build

猜你喜欢

转载自blog.csdn.net/wml00000/article/details/89073398