How to run forEach in AMD style?

760
2
Jump to solution
02-04-2014 10:03 AM
LeiZhou
New Contributor III
I want to create a checkbox in my left pane.  I try to understand the codes from the following sample,
http://developers.arcgis.com/javascript/sandbox/sandbox.html?sample=widget_legendvisible

It uses legacy code instead of AMD style.  I try to code in AMR mode e.g., use array.forEach() to replace dojo.forEach, But it does not workout.  Could you please have a look at my codes and give me some suggestion? Here is my code below: Thank you!
require(["esri/map",                "esri/geometry/Extent",                "esri/dijit/OverviewMap",                "esri/dijit/BasemapGallery",                "esri/arcgis/utils",                "esri/dijit/HomeButton",                "esri/layers/ArcGISTiledMapServiceLayer",                "esri/layers/ArcGISDynamicMapServiceLayer",                "esri/dijit/Legend",                "esri/dijit/Popup",                "esri/layers/FeatureLayer",                "esri/InfoTemplate",                 "dojo/dom",                "dojo/dom-construct",                "dojo/dom-class",                "dojo/_base/array",                "dojo/on",                "dojo/parser",                "dijit/registry",                "dijit/form/CheckBox",                "dijit/layout/BorderContainer",                "dijit/layout/ContentPane",                "dijit/TitlePane",                "dojo/domReady!"],           function (Map,                     Extent,                     OverviewMap,                     BasemapGallery,                     arcgisUtils,                     HomeButton,                     ArcGISTiledMapServiceLayer,                     ArcGISDynamicMapServiceLayer,                     Legend,                     Popup,                     FeatureLayer,                     InfoTemplate,                       dom,                     domConstruct,                     domClass,                     arrayUtils,                     on,                     parser,                     registry,                     CheckBox                     ){                            var map = new Map("mapDiv", {                   zoom: 3,                   basemap: "streets",                   sliderStyle: "large",                   sliderPosition:"top-right",                   logo: true,                   nav:false,                   navigationMode: 'css-transforms',                   autoResize: true,                                  });                                                         var dynaLayer1 = new ArcGISDynamicMapServiceLayer                   ("http://server.arcgisonline.com/arcgis/rest/services/Demographics/USA_Diversity_Index/MapServer",               {                   opacity: 0.8               });                              var dynaLayer2 = new ArcGISDynamicMapServiceLayer                   ("http://server.arcgisonline.com/arcgis/rest/services/Demographics/USA_Median_Age/MapServer",               {                   opacity: 0.8               });                //Add a legend               var layerInfo;               map.on("layers-add-result", function (evt) {                   layerInfo = arrayUtils.map(evt.layers, function (layer, index) {                       return { layer: layer.layer, title: layer.layer.name };                   });                                                        if (layerInfo.length > 0) {                       var legendDijit = new Legend({                           map: map,                           layerInfos: layerInfo                       }, "legendDiv");                       legendDijit.startup();                   }               });                map.addLayers([dynaLayer1, dynaLayer2]);                                  //Add a checkbox               var cb;               map.on("layers-add-result", function (results) {                    array.forEach(layerInfo, function (layer) {                       var layerName = layer.title;                       var checkBox = new CheckBox({                           name: "checkBox" + layer.layer.id,                           value: layer.layer.id,                           checked: layer.layer.visible,                           onChange: function (evt) {                               var clayer = map.getLayer(this.value);                               clayer.setVisibility(!clayer.visible);                               this.checked = clayer.visible;                           }                       }); //checkbox ends                   }); // forEach ends                });                var thinger = domConstruct.toDom("checkBox ");               domConstruct.place(thinger, dom.byId("toggle"), "after"); //toggle is the div id to put checkbox
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You're using array.forEach in your code, but in the function, you're assigning the alias "arrayUtils" for the array module "dojo/_base/array".

Try using "arrayUtils.forEach" instead. There was a similar question where I replied with a Fiddle that is written in AMD style.

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
You're using array.forEach in your code, but in the function, you're assigning the alias "arrayUtils" for the array module "dojo/_base/array".

Try using "arrayUtils.forEach" instead. There was a similar question where I replied with a Fiddle that is written in AMD style.
0 Kudos
LeiZhou
New Contributor III
Thank you very much!

You're using array.forEach in your code, but in the function, you're assigning the alias "arrayUtils" for the array module "dojo/_base/array".

Try using "arrayUtils.forEach" instead. There was a similar question where I replied with a Fiddle that is written in AMD style.
0 Kudos