I am using the dojo toolkit for this project. I have a drop down button which has a Menu as a child that then has various menu Items. Since this will be a "living" application, the menu items will change as the business grows. My aim here is to have the ability to create event listeners for all menu items contained by the Menu. Dojo provides an accessor function "getChildren()" that I use to produce an object of all Menu Items that I would like to loop through. The Menu item has both a label and id that I would like to be able to work with if and when that item is selected. (NOTE: Buttons, Menu and Menu Items are contained by another script but are stored globally in an array named layout, in the same way I append functions and vars to the main = [] bellow.)
main = [];
require([
"dojo/dom","dojo/on","dojo/parser","dojo/json","dojo/domReady!"
], function(dom,on,parser,JSON) {
parser.parse();
console.log("Main Start");
//flags
//Listeners
var gpMenuChildren = layout.gpMenu.getChildren();
for (i in gpMenuChildren) {
var id = gpMenuChildren[i].id;
var label = gpMenuChildren[i].label;
console.log(label);
console.log(id);
on(dom.byId(id),"click",function() {
console.log("dom node w/ 'id' of " + id +" was clicked!");
layout.gpButton.set("value",label);
main.updateQueryStr();
});
}
main.updateQueryStr = function updateQueryStr() {
console.log("updateQueryStr called...");
layout.cpCentTop.set("content", "Test updateQueryStr");
};
console.log("Main End");
});
Through console.log() i can confirm that the individual ids and labels are correct and we can successfully loop through the children through this method.
HERE IS THE PROBLEM
The event listeners are successfully connected to each item in the menu list, however the function component of the listener appears to be overwriting every time and consequently only takes on the properties (id and label) of the last child in the loop. In other words the same id and label values are recorded for every menu item when clicked.
Any thoughts as to ensure the creation of independent event listeners that can contain the appropriate values from the children?
Thanks in advance!