I don't know if I totally have my head wrapped around what you are trying to do. I get the feeling you are trying to load layers dynamically on application load using custom parameters.I start with an object containing the info I need to create the layer plus added functionality, either by making a server call that returns the object in json or a javascript object in a config .js file. Here's an example of the latter:var config = {
layersMapService: [
{
url: 'http://www.sample.com/arcgis/rest/services/some_layer/MapServer', //url
type: 'dynamic', //type (dynamic or tiled)
name: 'Some Layer', //name as shown in toc and elsewhere
id: 'somelayer', //id must be unique
visible: false, //initial visibility
opacity: 1, //initial opacity
imageFormat: 'png32', //image format
dpi: 96, //dpi
legend: true, //legend in toc and map
identify: false, //is layer available in identify widget
identifyLayers: [], //which layers
query: false, //is layer available in query widget
queryLayers: [] //which layers
}, {
url: 'http://www.sample.com/arcgis/rest/services/another_layer/MapServer',
type: 'dynamic',
name: 'Another Layer',
id: 'anotherlayer',
visible: false,
opacity: 0.5,
imageFormat: 'png32',
dpi: 96,
legend: true,
identify: true,
identifyLayers: [0,1,6],
query: true,
queryLayers: [0,1,3,4,5,6]
}
]
};
Then during your initialization function, after initializing your map iterate through the config.layersMapService object to add them to the map:var app = {
build: function() {
//build viewer, load mods, etc
//map
app.map = new esri.Map('map', {});
//add layers
dojo.forEach(config.layersMapService, app.layers.add.ms);
},
layers: {
add: {
ms: function(l) {
//l is the object with all the params
if (l.type === 'dynamic') {
var imageParameters = new esri.layers.ImageParameters();
imageParameters.format = l.imageFormat;
imageParameters.dpi = l.dpi;
var layer = new esri.layers.ArcGISDynamicMapServiceLayer(url, {
id: l.id,
imageParameters: imageParameters,
visible: l.visible,
opacity: l.opacity
});
/*
//because the layer is an object I add the params object
//directly to the layer object in case I want them
//again for some other functionality
//
//this is an important concept to really leverage the api
//
//for example: when the query widget gets the json object
//back from the server for each map service layer that information
//(fields, domains, etc) will be stored as an object in the
//corresponding layerIds object
*/
layer.layer_params = l;
app.map.addLayer(layer);
/*
//this is the time to do all the extra things
//while we have the layer variable
*/
app.toc.add(layer, l.legend);
if (l.identify) {
app.identify.add(layer, l.identifyLayers)
}
if (l.query) {
app.query.add(layer, l.queryLayers)
}
} else if (l.type === 'tiled') {
//add tiled layer
}
}
}
}
};
Don't know if this is what you're looking for, but hope it helps.PS: you'll have to clean up the 2nd piece of code; it wouldn't hold the indents for some reason.