Interacting with the JavaScript Map outside of jquery function()

3075
4
09-22-2015 12:02 PM
BWSCIM
by
New Contributor III

I'm wondering if its possible to interact with ESRI JavaScript API Map & Layers outside of the function(){} call?  I've tried adding graphics to the map.graphics layer but they don't appear.

Any help would be appreciated!

UPDATE: Ive been trying to add a graphics layer on a ASP MVC Ajax.BeginForm onComplete event.  If you define the onComplete event inside of function(){}/$(document).ready(...), it will be scoped to the function you pass to the ready function, and therefore invisible to ASP.NET MVC's JavaScript. When you define it outside of the function(){}/$(document).ready(...) function, it is global and available to ASP.NET MVC.

This answers why the result is occurring, My original question still stands.

0 Kudos
4 Replies
TyroneBiggums
Occasional Contributor III

You could create a global javascript namespace for your map object and have a global map object that way. Or, you could be a little less fancy and just var your map object globally. I have not seen any examples or direction to have an exposed map throughout the application otherwise.

EDIT: I'm assuming that you're using some kind of single page application framework b/c of MVC. If you're opening new pages and doing full post backs and such, you can lose your map object even if it's global.

0 Kudos
BWSCIM
by
New Contributor III

Thanks Tyrone

I have tried using a global variable to add graphics to the map graphicsLayer.  I do not get any error(s) but the graphics do not show up on the map.  So I'm still sifting through what I could be missing.

Yes - I am using MVC for this project and single page view with subviews.  JavaScript scope is certainly a challenge with MVC.

0 Kudos
TyroneBiggums
Occasional Contributor III

That really may be an MVC fail. I have a bit of a bias against MVC because I never gave it a real chance in any application I've worked on. I know enough about MVC that when we were assessing potential designs for our application, we dropped MVC because it was giving us too many fits. I'm not saying MVC is wrong, we just had no other use for it, and we were having issues. I'm sorry that I cannot be of more help on this one.

0 Kudos
BWSCIM
by
New Contributor III

After all is said and done I was doing it correctly.  Turns out I was bit by one of the oldest GIS bugs - Spatial Reference.  As my map was zoomed in to a specific location, I couldn't see that my code was adding graphics off the coast of Africa.  I did not recall that Graphics/Graphics Layers don't have a Spatial Reference - I added one to the base query and my graphics appears as expected in the location I expected them.