Select to view content in your preferred language

Legacy code vs AMD code

6149
19
08-29-2013 06:24 AM
GaneshSolai_Sambandam
Regular Contributor
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
JeffPace
MVP Alum
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


AMD.  legacy is deprecated at dojo 2.  we are rapidly approaching that.
0 Kudos
JasonZou
Frequent Contributor
I started one of my major projects using AMD, and I would highly recommend to use AMD if using v3.6 api with the following reasons.

  • With AMD style which will load the modules asynchronously, the initial loading performance will be greatly increased.

  • All JSAPI events are following dojo/on event mechanism now.

  • AMD will be enforced since dojo 2.0. Dojo is reaching v1.9 now, one more release away from 2.0!

  • The issue that your module may not work since one of the dependecies is not loaded won't exist if using AMD.

  • Minimize global variable usage.

  • and much more...


I can tell ESRI JSAPI team is working hard to make the conversion including the samples. There are some work for the improvement though for the JSAPI team especially on the documentation. I found out that when clicking any event handler (onLayerAddResult) link on AMD style, it won't work. I will have to switch to legacy event style to access the description.

Here are some references:
https://developers.arcgis.com/en/javascript/jshelp/inside_dojo_amd.html
http://dojotoolkit.org/documentation/tutorials/1.9/modules/
http://addyosmani.com/writing-modular-js/
0 Kudos
ZachLiu1
Deactivated User
0 Kudos
TracySchloss
Honored Contributor
I agree this is going to be a bumpy transition.  I have several projects written a couple years ago, it's not like I have the time to go back and rewrite everything I've done in the past.   I think there should be samples in both styles, so we can see the before and after and get the hang of the differences .  That API reference is good about showing both, but the samples are a mixed bag.  I'd hate to see every sample move to AMD because I need to see both to know where and how to change my older code.
0 Kudos
KenBuja
MVP Esteemed Contributor
I agree this is going to be a bumpy transition.  I have several projects written a couple years ago, it's not like I have the time to go back and rewrite everything I've done in the past.   I think there should be samples in both styles, so we can see the before and after and get the hang of the differences .  That API reference is good about showing both, but the samples are a mixed bag.  I'd hate to see every sample move to AMD because I need to see both to know where and how to change my older code.


This is one reason why I wish the Esri would keep the older versions of the samples and documentation on their website, similar to what Dojo does. With Dojo's documentation, you can select which version to see. However, when Esri redesigned their website, there doesn't seem to be a way of seeing the older versions (3.6 vs. 3.5 vs 3.4) of the help pages. It's kind of a pain to download the older SDKs and pick through the html files
0 Kudos
JasonZou
Frequent Contributor
I like to echo Ken. It's not only the samples need to have both versions, but to provide a way to access all released API as well. Lots of projects were developed under old versions, and when something goes wrong, there is no way to look at the API under which the project was developed. Many organizations aren't willing to take the effort to upgrade to the most recent version as long as the project still works.
0 Kudos
AdrianMarsden
Honored Contributor
Ok - with my major JSAPI project about to be fully live I thought I'd try to get my head around AMD, but failed, almost at the start.

For example the link https://developers.arcgis.com/en/javascript/jsapi/argument_aliases-amd.html on this page https://developers.arcgis.com/en/javascript/jstutorials/ 404s , so I am strugling to work out what exactly the function inside the require is for

<script>
  require(["esri/map"], function(Map) { ... });
</script>


For a simple map it seems only to define the map, but WTF is this all about?

function(dom, domAttr, array, Color, parser, esriConfig, Map, Graphic, GeometryService, BufferParameters, Draw, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol)



So, I'm giving up for now, and my code will be a horrid mix for a while.

I may return later when there are better guides

ACM
0 Kudos
ZachLiu1
Deactivated User
first of all, require is just a function.:)

It takes two args.

The first one is an array of all modules you needed for your project. For a Ags API app, it usually includes Dojo modules and ESRI modules.

The second argument is a function whose arguments pair with the modules you required. All arguments in this function (may be a long list) are just references to your loaded modules and you can give whatever names you want, but usually it's a good idea to follow conventions.

It will be better to read this first.

http://dojotoolkit.org/documentation/tutorials/1.9/modern_dojo/
0 Kudos
AdrianMarsden
Honored Contributor
Thanks - that helps a bit- amazingly just having just the modules required and a minimal second arg it still allworks - I guess the rest still being old style just picks up.

Any idea where the link should go to to show "preferred argument aliases for Esri modules" which I guess is the list I add to the second arg for all the modules I am referencing?

ACM
0 Kudos