Reputation: 2604
I have a menubar object, one of the menus having submenus. I wanna when i click some of the submenus to replace with it's icon the parent of the selected submenu icon.
Upvotes: 0
Views: 552
Reputation: 2614
Menubar in Flex 4/4.5 is still an mx component that's from Flex 3 era.
Menubar is data-driven, so you can modify the parent menu's corresponding data item, and change its icon data, and reassign the entire menu data back to the component. The following pseudo code may represent the logic as described:
private var _menuBarData:Object;
public var myMenuBar:MenuBar;
override protected function createChildren():void
{
super.createChildren();
if ( myMenuBar == null )
{
myMenuBar = new MenuBar();
addChild(myMenuBar);
}
myMenuBar.dataProvider = _menuBarData; // assume that _menuBarData is populated already
myMenuBar.removeEventListener(MenuEvent.CHANGE, myMenuBar_change);
myMenuBar.addEventListener(MenuEvent.CHANGE, myMenuBar_change);
}
private function myMenuBar_change( event:MenuEvent ):void
{
var itemData:Object = event.item;
if ( itemData == null )
{
return;
}
var iconData:Object = itemData[iconField];
var parentData:Object = findParentData(itemData);
if ( parentData )
{
parentData[iconField] = iconData;
event.menuBar.dataProvider = _menuBarData; // reassign _menuBarData as it is updated
}
}
private function findParentData(itemData:Object):Object
{
// traverse through _menuBarData object and find out the parent of itemData
// this highly depends on how your data structure is designed
}
Upvotes: 2