map vs app.map

766
2
Jump to solution
08-20-2012 06:12 AM
p_thibault
New Contributor
In the new print widget the examples show the map being defined as:
var app = {};
app.map = null,
app.map = new esri.Map("map", {
          extent: initialExtent,
          wrapAround180: true
        });

this is compared to just :
map = new esri.Map.........

Can some please explain what is happening with the variable "app"?

Thanks.
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor
It looks like the sample was written to keep application specific objects in their own namespace.

For example, when you write
var map = new esri.Map("map");


'map' is bound to the Window object of the browser, which is basically the global namespace.
By declaring
var app = {}; // This is an empty object, equivalient to window.app app.map = new esri.Map("map"); // map is now bound to window.app.map


In the print widjet example, all the application specific values are now in window.app (map, printer, symbols, toolbar).
It's a method of organizing the code in the application.

It's interesting that it's done in the Print Widget, I don't think I've seen it in other esri js samples before. It's not a strict rule, more of a best practices kind of thing to keep the global namespace clean. It's not mentioned their coding guidelines. In this case maybe there are othe common libraries that may already have a window.printer object defined and they wanted to avoid overrriding it.

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor
It looks like the sample was written to keep application specific objects in their own namespace.

For example, when you write
var map = new esri.Map("map");


'map' is bound to the Window object of the browser, which is basically the global namespace.
By declaring
var app = {}; // This is an empty object, equivalient to window.app app.map = new esri.Map("map"); // map is now bound to window.app.map


In the print widjet example, all the application specific values are now in window.app (map, printer, symbols, toolbar).
It's a method of organizing the code in the application.

It's interesting that it's done in the Print Widget, I don't think I've seen it in other esri js samples before. It's not a strict rule, more of a best practices kind of thing to keep the global namespace clean. It's not mentioned their coding guidelines. In this case maybe there are othe common libraries that may already have a window.printer object defined and they wanted to avoid overrriding it.
0 Kudos
derekswingley1
Frequent Contributor

In the print widjet example, all the application specific values are now in window.app (map, printer, symbols, toolbar).
It's a method of organizing the code in the application.


You got it. Nothing more to it... trying to avoid polluting the global namespace. To answer the quasi-question, I didn't run into any conflicts with existing object/libraries while writing that sample.
0 Kudos