Cmoslem
Cmoslem

Reputation: 23

Why does Alert.show() give me two Alert boxes?

I have a strange behavior when I use Alert.show in a function that takes a DataGridEvent.

When the Alert.show is executed the function runs twice.

This is the code:

private function onItemEnd(event:DataGridEvent):void
{

    var pattern:RegExp =/[^a-z0-9A-ZéèçàêïôëËÉÊÈìíîïÌÍÎÏÇùúûü]/;
    var res:Boolean=pattern.test(texteSaisi);
    if(res==true){
        event.reason = DataGridEventReason.CANCELLED; 
        Alert.show("Le nom de l'option ne doit pas contenir des caractère spéciaux."); 
    }else{ 
        if (event.dataField == "libelle"){
            question.libelle = texteSaisi;
        }else if (event.dataField == "description") {
            question.description = texteSaisi;
        }

<mx:DataGrid 
    draggableColumns="false" 
    dataProvider = "{model.obtenirListeOptionsTarificationProposition}" 
    id = "gridOptions"
    rowCount = "10" 
    doubleClickEnabled = "true"
    itemClick = "onItemClick(event)" 
    change = "onChange(event)"
    width = "80%" 
    editable = "true"
    itemEditEnd = "onItemEnd(event)">

When this function is called I have two alerts on the screen I don't know why.

Upvotes: 2

Views: 722

Answers (2)

Cmoslem
Cmoslem

Reputation: 23

I fixed the problem by setting a boolean to true when the alert is fired the first time then i set it to false in the on click handler if the user try to change another item in onter row.

Thx for the help ;)

Upvotes: 0

Sunil D.
Sunil D.

Reputation: 18194

I suspected that using an Alert in conjunction with the itemEditEnd event is problematic. Wrote a simple test case, which behaves differently (mine seems to go into an endless loop).

The problem is this:

  1. The itemEditEnd event can be dispatched in numerous ways: keyboard interaction (tabbing or pressing enter while editing a cell), mouse interaction, etc.

  2. I start editing a cell. Then I hit tab/enter or click in another cell. This generates the itemEditEnd event and moves the focus to a new editable cell.

  3. In the event handler for the itemEditEnd event, an Alert is displayed. This pop up removes focus from that editable cell ... thus dispatching a second itemEditEnd event.

In my test app below, I have actually created an inifinite loop.

If you need to display an alert after the itemEditEnd event, maybe you can put some logic in the event handler (so it returns immediately the second time).

<?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"
               creationComplete="onCreationComplete()">

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

            private function onCreationComplete():void
            {
                grid.dataProvider=createCollection();
            }


            private function createCollection():ArrayCollection
            {
                var tmp:Array = [];
                for (var i:int = 1; i<11; i++)
                {
                    tmp.push({ data: Math.random()*100, label: i.toString() + " label" });
                }
                return new ArrayCollection(tmp);
            }

            private function onItemEditEnd():void
            {
                Alert.show("item edit end");
            }

        ]]>
    </fx:Script>

    <mx:DataGrid id="grid" itemEditEnd="onItemEditEnd()" editable="true" />

</s:Application>

Upvotes: 2

Related Questions