Legacy code vs AMD code

5744
19
08-29-2013 06:24 AM
GaneshSolai_Sambandam
New Contributor III
Hi GIS folks,
I am really confused with the legacy code vs AMD code. Although, I switched to AMD style of coding  in my application and some of the sample/example codes in the ARCGIS javascript api are still in legacy code, because of which I am struggling to run my application as most of the time i end up getting errors. can anyone recommend which style of coding should we follow with v3.6 api.

Regards
Ganesh
0 Kudos
19 Replies
ZachLiu1
Occasional Contributor II
If you don't provide args for second function, you are still using dojo modules loaded in traditional way, which exist in the global scope.

The require function creates a local scope, so you can not get access to the modules outside of the require function. The major benefit is your global object is clean.

e.g.

you can write
require(["esri.map"], function(Map) { 
    var map = new Map();
});


or
you can still write
require(["esri.map"], function(Map) { 
    var map = new esri.map();
});


But they work in different ways.

Actually, if you don't load ArcGIS API which automatically load many modules in the old way I guess, the second one won't work.
0 Kudos
AdrianMarsden
Occasional Contributor III
😉 Er, sorry for sounding silly, but aren't those two code blocks identical?  I think I know what you mean.  Ah I see it now, the Upper case M in the first one and no 'esri'.  Doh!

Anyway, it's Friday afternoon where I am, it is sunny and will remain so for the weekend, so I'm off home!
0 Kudos
ReneRubalcava
Frequent Contributor
Not to pimp, but I'll pimp a little, you can view a presentation I did at the last dev summit on AMD.
http://video.esri.com/watch/2318/embrace-your-modules-on-amd-and-javascript

There was another good talk about building modular dijits, but doesn't look like it's available.

The transition to AMD from the (ArcGIS JS) pre-3.0 days can be a little bumpy, and it may not be perfect, but it works pretty well.

I am however of the opinion that when you have a dependency array like
function(dom, domAttr, array,Color, parser, esriConfig,Map,Graphic,GeometryService,BufferParameters,Draw,SimpleMarkerSymbol,SimpleLineSymbol,SimpleFillSymbol)

It's a good sign you could use a refactor of some sort.
0 Kudos
AdrianMarsden
Occasional Contributor III
<lightbulb>  (well, low energy, dimmer light bulb, that has occasional brown-outs)  I'm sort of seeing it now.  Gives me an excuse to totally re-structure my code as I move each bit over from my main init function
0 Kudos
CharlesGeiger
New Contributor III
I, too, am struggling to convert older code to AMD, and echo many of the earlier posts in this thread, especially requesting that Esri show examples in both legacy and AMD format to help us with the transition.  My particular question is why all of the examples involving dynamic map service layers are still in legacy code.  Is there some move to separate dynamic map services from tiled and feature services?
0 Kudos
AdrianMarsden
Occasional Contributor III
OK - slowly making progress, but I am hitting (what I think) are scope issues.

I am creating an overview map.  I have the require and attribute all done -

require(["esri/map", "esri/layers/ArcGISTiledMapServiceLayer",
 "esri/layers/ArcGISDynamicMapServiceLayer",
"esri/geometry/Extent",
"esri/layers/FeatureLayer",
"agsjs/dijit/TOC",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol",
 "dojo/_base/Color",
 "esri/dijit/OverviewMap",
 "dojo/parser",
"dojo/domReady!"
], function (
          Map,
          ArcGISTiledMapServiceLayer,
          ArcGISDynamicMapServiceLayer,
          Extent,
          FeatureLayer,
          TOC,
          SimpleFillSymbol,
          SimpleLineSymbol,
          Color,
          OverviewMap,
          parser
          ) {



then I create the ovmap

 var overviewMapDijit = new OverviewMap({        map: map,
        expandFactor: 2,
        visible: true,
        height: 230,
        width: 230
    });


but get

Uncaught TypeError: Cannot read property 'wkid' of undefined 


As if the map doesn't exist - I have a "var map;" earlier of course - what I have, is, more or less the same as https://developers.arcgis.com/en/javascript/jssamples/widget_overviewmap.html

c
heers

ACM
0 Kudos
JeffPace
MVP Alum
have you added layers to your map before creating the overview?
0 Kudos
AdrianMarsden
Occasional Contributor III
Yep - Hjelmeland has been PMing me with lots of suggestions -  on the last go we took working code of his and dropped my map service in.  It worked on his code.  The only difference I can see is if I examine the map object, on the working code we have

spatialReference: Object
latestWkid: 27700
wkid: 27700
__proto__: Object
updating: false
width: 1680
wrapAround180: fal


On mine

spatialReference: Objectwkid: 27700
__proto__: Object
updating: false
width: 1435
wrapAround180: false




for the SpatialReference object - you'll notice the additional "latestWkid" attribute.

Cheers

ACM

Edit nearly there - my code was a horrid mix of legacy and AMD, and I had a new AMD style chunk basically doing the same stuff I'd inserted ages back.  Removing this gives me an overview map (breaks loads of other stuff until I reinsert it al in the right place)

Edit #2 No idea - a brief glimpse was lost again 😞 will still work on it.
0 Kudos
AdrianMarsden
Occasional Contributor III
I gave it a good try, but for now defeated.  I worked out my wkid issue was something to do with my local 3.5 - running it with esr 3.6 moved on to the next errors - a shed load of parser issues.

So, I will put this aside for now, and concentrate on stuff my users are asking for - like a constrained Floatingpane 🙂

Thanks for the help so far - at least I have a better understanding.

ACM
0 Kudos
JohnGravois
Frequent Contributor
we published a blog post the other day comparing an identical sample written in AMD and legacy patterns.  hope its helpful!

abc's of AMD
http://blogs.esri.com/esri/arcgis/2013/10/14/the-abcs-of-amd/
0 Kudos