Your example gets most of the job done. The two issues I found were where the LayerList put the basemap at the top of the list, and sometimes the ArcGIS Online basemap title in the LayerList didn't match the title given in the web map.
- var layers = arcgisUtils.getLayerList(response);
- array.forEach(response.itemInfo.itemData.baseMap.baseMapLayers, function(l){
- layers.unshift({
- layer: l.layerObject
- });
- });
If you added your basemap as a "layer from the web", and didn't save the layer to your ArcGIS Online Account, the name generated by the layer list may not match the name from the web map. Here's another patch to make that basemap layer title show.
- var layers = arcgisUtils.getLayerList(response);
- array.forEach(response.itemInfo.itemData.baseMap.baseMapLayers, function(l, index){
- var layerData = {
- layer: l.layerObject
- };
- if (index === 0) {
- layerData.layer = lang.mixin(layerData.layer, {title: response.itemInfo.itemData.baseMap.title});
- }
- layers.unshift(layerData);
- });
It doesn't look pretty, but it works.
JS Bin - Collaborative JavaScript Debugging