scoping conumdrum
2013-05-18 17:38:14 GMT
Hello All,
I'm wrestling with a bit of a puzzle related to scoping in the code below. Setting up the menu items and their onClick handlers works fine, but when I try to call the same showBasemap() method during postCreate() that the onClick uses, I'm unable to find the menu item.
Thanks for any help!
--john
define(["dojo/_base/declare","dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin", "dijit/_OnDijitClickMixin", "dijit/Toolbar", "dijit/form/Button", "dojo/_base/lang", "dojo/_base/array", "dijit/form/DropDownButton", "dijit/Menu", "dijit/ToolbarSeparator", "dijit/MenuItem", "dijit/CheckedMenuItem", "dojo/text!./templates/BasemapToolbar.html"],
function(declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _OnDijitClickMixin, Toolbar, Button, lang, array, DropDownButton, Menu, ToolbarSeparator, MenuItem, CheckedMenuItem, template ){
return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _OnDijitClickMixin], {
templateString: template,
_basemaps: null,
baseClass: "basemapToolbar",
layerCollection: null,
//constructor for parent called before constructor of child class
constructor: function(arguments) {
this.layerCollection = arguments.layerCollection;
this._basemaps = this.layerCollection.basemaps;
},
showBasemap: function(selectedIndex) {
//only one basemap showing at a time
array.forEach(this._basemaps, function(basemap, idx) {
if (selectedIndex == idx) {
array.forEach(basemap.services, function(targetId){
this.layerCollection.getLayerById(targetId).setVisibility(true);
this.basemapMenu.getChildren()[idx].containerNode.style.fontWeight = "bold";
}, this);
} else {
array.forEach(basemap.services, function(targetId){
this.layerCollection.getLayerById(targetId).setVisibility(false);
this.basemapMenu.getChildren()[idx].containerNode.style.fontWeight = "normal";
}, this);
}
}, this);
},
postCreate: function() {
//add menu items to basemap menu
array.forEach(this._basemaps, function(item, idx) {
this._addBasemapMenuItem(this.basemapMenu, item, idx, this);
if (item.default) {
//throws exception re: undefined menuItem
this.showBasemap(idx);
}
}, this);
},
_addBasemapMenuItem: function(menu, item, idx, parent) {
menu.addChild(new MenuItem({
label: item.label,
onClick: function() {
parent.showBasemap(idx);
}
}));
}
});
}
);
________________________________________________________ Dojo Toolkit: http://dojotoolkit.org/ Tutorials: http://dojotoolkit.org/documentation/ Reference Guide: http://dojotoolkit.org/reference-guide API Documentation: http://dojotoolkit.org/api Dojo-interest <at> mail.dojotoolkit.org To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
RSS Feed