2012-12-17 2 views
0

ArrayCollection dataProvider가있는 목록이 있습니다. 내 프로그램에는 사용자가 목록의 selectedIndex에 대한 기능을 수행하기 위해 클릭 할 수있는 버튼이 있지만 먼저 액션을 수행 할 것인지 묻는 경고가 표시됩니다. 사용자가 Alert에 응답하면 해당 목록의 selectedIndex에 대한 작업이 수행됩니다.경고 후 목록의 selectedIndex

내 문제는 selectedItem = -1 명확하게 선택 되었음에도 불구하고 Alert 윈도우 CloseEvent 이후입니다. Alert CloseEvent 코드에서 validateNow()를 수행하여이 문제를 해결했습니다.

내 질문 : 왜이 작업을 수행해야하며 잘못 되었습니까? 또는이 정상/모범 사례입니까? 또한 try-catch를 사용하는 것 외에 어떤 것이 선택되었는지 확인하기 위해 List를 검사하는 것이 더 좋습니다. 아무 것도 선택하지 않으면 최종 사용자가 생성 된 오류를보고 싶지 않습니다.

코드 : 당신이로 validateNow()을 꺼내 경우가 selectedIndex의은 -1 생각하기 때문에

//Note: "fl" is a class with "friendsList" bindable ArrayCollection; for the sake of keeping this short I will not include it 

private function _removeFriendClick(event:MouseEvent):void 
{ 
    try { 
     if (this.friendsList.selectedIndex != -1) { 
      Alert.show("Are you sure you want to remove "+this.fl.friendsList[this.friendsList.selectedIndex].label+" as a friend?", "Remove Friend", Alert.YES | Alert.CANCEL, this, this._removeFriendConfirm, null, Alert.CANCEL); 
     } 
    } catch (e:Error) { } 
} 

private function _removeFriendConfirm(event:CloseEvent):void 
{ 
    this.friendsList.validateNow(); 
    trace(this.friendsList.selectedIndex); 
} 

그래서, 위의 코드와 함께, 예외가 발생합니다.

+1

이것은 정상적인 동작은 아니지만 표시되는 코드는 상황을 평가하는 데 충분하지 않습니다. 함께 일할 수있는 기회를 좀 더 주어야합니다. – RIAstar

+0

경고 창 앞에 선택한 인덱스를 저장하지 않으시겠습니까? – Anton

+0

@RIAstar 포함시킬 항목이 무엇인지 잘 모르겠습니다. – Lyynk424

답변

1

나는이 방법으로 그것을 할 것입니다 : 당신이 당신의 핸들러를 호출하기 직전에

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 

<fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 
     import mx.controls.Alert; 
     import mx.events.CloseEvent; 

     [Bindable]private var friendsList:ArrayCollection = new ArrayCollection([{data:"111", label:"Don"}, {data:"222", label:"John"}]); 

     private function onBtnRemove():void 
     { 
      laFriend.text = ""; 

      try 
      { 
       if (cbFriends.selectedIndex != -1) 
       { 
        Alert.show("Are you sure you want to remove " + cbFriends.selectedItem.label + " as a friend?", "Remove Friend", Alert.YES | Alert.CANCEL, this, this._removeFriendConfirm, null, Alert.CANCEL); 
       } 
      } catch (e:Error) { } 
     } 

     private function _removeFriendConfirm(event:CloseEvent):void 
     { 
      laFriend.text = "Selected friend: " + cbFriends.selectedItem.label; 
     } 
    ]]> 
</fx:Script> 


<mx:VBox> 
    <s:ComboBox id="cbFriends" dataProvider="{friendsList}"/> 
    <s:Button id="btnRemove" label="Remove" click="onBtnRemove()"/> 
    <s:Label id="laFriend" text=""/> 
</mx:VBox> 

</s:Application> 
0

당신은 선택을 수행합니까?

selectedIndex를 설정하면 라이브 사이클 때문에 즉시 다시 가져올 수 없습니다. 값을 읽으려면 커밋해야합니다.

귀하의 validateNow가 강제로 적용됩니다. 그러나 나중에 수동으로 시행하지 않아도됩니다.

관련 문제