PDA

View Full Version : Why is itemClickHandler being called twice for this popbutton


CDHBookingEdge
12-05-2006, 04:18 PM
Here's the code, question(s) to follow after:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" frameRate="120" width="100%" height="100%" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.*;
import mx.controls.*;

private const SEARCH_GOOGLE:int = 0;
private const SEARCH_WIKIPEDIA:int = 1;
private const SEARCH_DICTIONARY:int = 2;
private var myMenu:Menu;
private var nSearchType:int = SEARCH_GOOGLE;

private function doSearch(strSearch:String) : void
{
if (strSearch == "")
{
Alert.show("The entry cannot be empty");
return;
}
var strUrl:String = "";
Alert.show("doSearch -> nSearchType = " + nSearchType);
switch (nSearchType)
{
case SEARCH_GOOGLE:
strUrl = getGoogleUrl(strSearch);
break;
case SEARCH_WIKIPEDIA:
strUrl = getWikipediaUrl(strSearch);
break;
case SEARCH_DICTIONARY:
Alert.show("I don't know how to search the dictionary yet");
break;
}
if (strUrl != "")
navigateToURL(new URLRequest(strUrl), '_blank');
}

private function getGoogleUrl(strSearch:String) : String
{
var strUrl : String = "http://www.google.com/search?hl=en&q=" + strSearch;
return strUrl;
}

private function getWikipediaUrl(strSearch:String) : String
{
var strUrl : String = "http://en.wikipedia.org/wiki/" + strSearch;
navigateToURL(new URLRequest(strUrl), '_blank');
return strUrl;
}

private function LookupInDictionary(strWord:String) : void
{
Alert.show("Gonna find this for ya in the dictionary " + strWord);
}
// Initialize the Menu control, and specify it as the pop up object
// of the PopUpButton control.
private function initSearchButtonMenu():void
{
myMenu = new Menu();
var dp:Object = [{label: "Google"}, {label: "Wikipedia"}, {label: "Dictionary"}];
myMenu.dataProvider = dp;
myMenu.selectedIndex = 0;
myMenu.addEventListener("itemClick", itemClickHandler);
searchButton.popUp = myMenu;
searchButton.label = "Search: " + myMenu.dataProvider[myMenu.selectedIndex].label;
}

// Define the event listener for the Menu control's itemClick event.
private function itemClickHandler(event:MenuEvent):void
{
var label:String = event.item.label;
searchPod.title = String("Search " + label);
searchButton.label = String("Search: " + label);
searchButton.close();
myMenu.selectedIndex = event.index;
Alert.show("myMenu.selectedIndex = " + myMenu.selectedIndex);
nSearchType = myMenu.selectedIndex;
Alert.show("nSearchType = " + nSearchType);
}

]]>
</mx:Script>
<mx:Panel id="searchPod">
<mx:VBox>
<mx:TextInput id="src" text=""/>
<mx:PopUpButton id="searchButton" label="Search" creationComplete="initSearchButtonMenu();" click="doSearch(src.text);" width="135" />
</mx:VBox>
</mx:Panel>
</mx:Application>


Ok now if you run this (I've made sure it runs in the Online Flex Compiler (http://try.flex.org/)) you'll see the alerts in itemClickHandler being shown twice once showing -1 values and once showing non-negative values. Then if you click on the button, you'll see it tell you the nSearchType is -1. Which, if you follow the alerts is not true it is a positive value.

Now my questions are why is itemClickHandler called twice, and why is nSearchType being noted as -1 on response to the click event?

Testing Steps:
1) Bring up program
2) Change the selection in the popupbutton
3) type in string to search.
4) Click on the popupbutton.

Thanks in advance,
Christopher

ripvtech
12-05-2006, 07:12 PM
Chris,

I did a little reading up on this. I've had the same problem.

Go to this google groups discussion (http://groups.google.com/group/netscape.public.mozilla.xbl/browse_thread/thread/552bb5a3a71c4cd0/9f5541565b330e%239f5541565b330e) scroll down to the 2nd post (Neil's) he explains it pretty well. I also checked out Adobe's example code and it indeed ran the eventlistener twice.

With that being said:

I have changed your code to use a "PopUpMenuButton"
heh, it even uses less code, which is alway a good thing IMHO :D


private function initSearchButtonMenu():void
{
var dp:Object = [{label: "Google"}, {label: "Wikipedia"}, {label: "Dictionary"}];
searchButton.dataProvider = dp;
searchButton.addEventListener(MenuEvent.ITEM_CLICK , itemClickHandler);
}

// Define the event listener for the Menu control's itemClick event.
private function itemClickHandler(event:MenuEvent):void
{
var label:String = event.item.label;
searchPod.title = String("Search " + label);
searchButton.label = String("Search: " + label);
searchButton.close();
trace(event.index); //Alert.show(event.index);
}

]]>
</mx:Script>
<mx:Panel id="searchPod">
<mx:VBox>
<mx:TextInput id="src" text=""/>
<mx:PopUpMenuButton id="searchButton" label="Search" creationComplete="initSearchButtonMenu();" click="doSearch(src.text);" width="135" />
</mx:VBox>
</mx:Panel>




Take care,
JD

CDHBookingEdge
12-05-2006, 08:15 PM
Coolness! I'll take a look at it in a bit.

ripvtech
12-08-2006, 04:54 PM
Well I just got an email back from Stephen Gilson over at Adobe and it seems that the issue will be fixed in the 2.0.1 release.

CDHBookingEdge
12-08-2006, 07:45 PM
Cool at least they're aware and planning a fix LOL.