學(xué)習(xí)要點(diǎn):
1、dojo框架;
2、dojo框架下類的定義與繼承;
3、dojo資源:
http:///api/
http:///download/
http:///documentation/tutorials/1.10/hello_dojo/
最近在研究arcgis js api,但好像不可避免要遇到dojo框架的學(xué)習(xí)與使用,因?yàn)閍rcgis js api就是基于dojo開發(fā)的。
思路:1、本地部署好arcgis js api(過程略);2、dojo簡(jiǎn)單實(shí)例代碼;3、dojo類的定義與繼續(xù);
一、官網(wǎng)實(shí)例
實(shí)例1:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Tutorial: Hello Dojo!</title>
- </head>
- <body>
- <h1 id="greeting">Hello</h1>
- <!-- load Dojo -->
- <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
- data-dojo-config="async: true"></script>
- </body>
- </html>
這個(gè)實(shí)例中引用了dojo CDN官網(wǎng)api,試了下,并不好用,還是換成自己本地部署的api好使;
實(shí)例2
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Tutorial: Hello Dojo!</title>
- </head>
- <body>
- <h1 id="greeting">Hello</h1>
- <!-- load Dojo -->
- <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
- data-dojo-config="async: true"></script>
-
- <script>
- require([
- 'dojo/dom',
- 'dojo/dom-construct'
- ], function (dom, domConstruct) {
- var greetingNode = dom.byId('greeting');
- domConstruct.place('<em> Dojo!</em>', greetingNode);
- });
- </script>
- </body>
- </html>
輸出結(jié)果為:Hello Dojo!
實(shí)例3,類的定義與調(diào)用。定義一個(gè)js類,然后在html頁(yè)面中調(diào)用

myModule.js
- define([
- // The dojo/dom module is required by this module, so it goes
- // in this list of dependencies.
- 'dojo/dom'
- ], function(dom){
- // Once all modules in the dependency list have loaded, this
- // function is called to define the demo/myModule module.
- //
- // The dojo/dom module is passed as the first argument to this
- // function; additional modules in the dependency list would be
- // passed in as subsequent arguments.
-
- var oldText = {};
-
- // This returned object becomes the defined value of this module
- return {
- setText: function (id, text) {
- var node = dom.byId(id);
- oldText[id] = node.innerHTML;
- node.innerHTML = text;
- },
-
- restoreText: function (id) {
- var node = dom.byId(id);
- node.innerHTML = oldText[id];
- delete oldText[id];
- }
- };
- });
hellodojo.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Tutorial: Hello Dojo!</title>
- </head>
- <body>
- <h1 id="greeting">Hello</h1>
- <!-- configure Dojo -->
- <script>
- // Instead of using data-dojo-config, we're creating a dojoConfig
- // object *before* we load dojo.js; they're functionally identical,
- // it's just easier to read this approach with a larger configuration.
- var dojoConfig = {
- async: true,
- // This code registers the correct location of the "demo"
- // package so we can load Dojo from the CDN whilst still
- // being able to load local modules
- packages: [{
- name: "demo",
- location: location.pathname.replace(/\/[^/]*$/, '') + '/demo'
- }]
- };
- </script>
- <!-- load Dojo -->
- <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
-
- <script>
- require([
- 'demo/myModule'
- ], function (myModule) {
- myModule.setText('greeting', 'Hello Dojo!');
-
- setTimeout(function () {
- myModule.restoreText('greeting');
- }, 3000);
- });
- </script>
- </body>
- </html>
在上面這個(gè)實(shí)例中,實(shí)現(xiàn)了js類的定義與調(diào)用,但在html頁(yè)面中有個(gè)段代碼需要注意,沒有這段代碼無法實(shí)現(xiàn)調(diào)用
- <script>
- // Instead of using data-dojo-config, we're creating a dojoConfig
- // object *before* we load dojo.js; they're functionally identical,
- // it's just easier to read this approach with a larger configuration.
- var dojoConfig = {
- async: true,
- // This code registers the correct location of the "demo"
- // package so we can load Dojo from the CDN whilst still
- // being able to load local modules
- packages: [{
- name: "demo",
- location: location.pathname.replace(/\/[^/]*$/, '') + '/demo'
- }]
- };
- </script>
二、類的定義與繼承(http:///reference-guide/1.10/dojo/_base/declare.html)實(shí)例請(qǐng)參見官網(wǎng)

cacheMap.js
- /**
- * Created by neil on 2015/8/27.
- */
- define(["dojo/_base/declare",
- "esri/layers/ArcGISTiledMapServiceLayer",
- "esri/SpatialReference",
- "esri/geometry/Extent",
- "esri/layers/TileInfo"], function (declare, ArcGISTiledMapServiceLayer, SpatialReference, Extent, TileInfo) {
- return declare(ArcGISTiledMapServiceLayer, {
- constructor: function (baseUrl) {
- this.baseUrl=baseUrl;
- }
- });
- });
dojo5.html
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title>這是測(cè)試瓦片地圖的一個(gè)類</title>
- <script>
- var dojoConfig = {
- async: true,
- packages: [{
- name: "demo",
- location: location.pathname.replace(/\/[^/]*$/, '') + '/demo'
- }]
- };
- </script>
- <script src="http://192.168.1.51/arcgis_js_api/library/3.14/3.14/init.js"></script>
- <script>
- require(["demo/cacheMap"], function (cacheMap) {
- var googleMapLayer = new cacheMap("baseUrl");
- greeting.innerHTML=googleMapLayer.baseUrl;
- });
- </script>
- </head>
- <body>
- <h1 id="greeting">Hello</h1>
- </body>
- </html>
未完
|