7 Replies Latest reply on Feb 28, 2012 2:31 PM by Flex harUI

    Why is this handler called 3x?

    JackHombre Level 1

      I have this handler that is called 3x when I think it should only be called once. What I have is an mx:DataGrid and for some reason, when I edit a particular control, it calls the event hander 3x. It's a big mystery why it keeps getting called. How do I investigate why these calls get made?

      The grid is defined by

      <mx:DataGrid id="dg" dataProvider="{ac}" width="100%" height="100%" editable="{true}" itemEditBeginning="disableEditing(event);" showHeaders="false" itemEditEnd="save(event);" rowCount="{ArrayCollection(dg.dataProvider).length}">

      and save() looks like:

      private function save(event:DataGridEvent):void {

      trace("Called save cuz: " + event.toString() + " and " + event.reason);

      What I get out of a call to that is:

      Called save cuz: [Event type="itemEditEnd" bubbles=false cancelable=true eventPhase=2] and newRow

      Called save cuz: [Event type="itemEditEnd" bubbles=false cancelable=true eventPhase=2] and other

      Called save cuz: [Event type="itemEditEnd" bubbles=false cancelable=true eventPhase=2] and other

      Incidentally, the code for this handler (not written by me) seems to be prepared for one superfluous call, since it tests a condition early in the function and then returns. This happens during that second event.reason == "other" call. However, my question still stands: how do I investigate why each call is made? event.reason doesn't tell me very much.

        • 1. Re: Why is this handler called 3x?
          JackHombre Level 1

          And I'm using Flex 3.5 and Adobe Flash Builder 4.0.1

          • 2. Re: Why is this handler called 3x?
            JackHombre Level 1

            I've come to understand the problem a bit better: it seems that when I edit the cell, then push enter, save() gets called 3x and one of those times has event.reason == "newRow". If after editing the cell I click somewhere outside the cell, then save() gets called twice, and both times event.reason == "other". Oh yeah, and when I push enter, the event.target seems to be for the cell below.

            By now I feel that my problem is that I just don't know what I'm doing. The handler gets called a bunch of times and I have no idea how to know why it's being called or if I should ignore what. I was hoping that combinations of calls to:

            event.stopImmediatePropagation();

            event.stopPropagation();

            Would work (the idea is that I stop all these parasitic calls) but they don't do anything. I've read tutorials and such, but they don't seem to help me w/this problem.

            • 3. Re: Why is this handler called 3x?
              Flex harUI Adobe Employee

              Maybe an issue with a custom renderer/editor?  If you use default renderer/editor does it still get called multiple times?

              • 4. Re: Why is this handler called 3x?
                JackHombre Level 1

                Not sure how to answer that. The declaration of the column is as follows:

                <mx:DataGridColumn headerText="Value" dataField="parm_value" itemEditor="DataEditor" editorDataField="newData" sortable="false"  />

                Now, DataEditor.mxml will create a different control, depending on which row on the grid is involved, some of which are custom and others are mx:ComboBoxes or mx:TextInputs or whatever. I just ran some tests, and it seems they behave the same with primitive controls as they do with user-defined ones.

                 

                The solution I have in place now sets a flag, which causes the handler (save()) to return immediate, if it is set. I can do this, because the handler throws up an Alert.show(), confirming that the user wants to commit the change. Then, I reset the flag in Alert.show()'s handler. I suppose, the only race condition here could be that the user might be so johnny-on-the-spot that he'd confirm before the other calls to save() could be made, thereby resetting the flag early. As far as I know, that's not impossible. (But as far as I know, the call to Alert.show()'s hander gets put into a queue, behind the other calls to save()) In any case, this is obviously not a generally acceptable solution, and if there isn't one, then someone should be shot.

                • 5. Re: Why is this handler called 3x?
                  Flex harUI Adobe Employee

                  The column has a custom editor defined in DataEditor.mxml.  The entire grid has an itemEditEnd handler.  If there is another column without a custom editor, does it exhibit the same problem?  Or reconfigure this column to use the default editor.  That will help determine if the problem is in DataEditor.mxml

                   

                  If DataEditor.mxml is a container of an actual editable control, it should properly implement IFocusManagerComponent otherwise you might see symptoms as you have described.

                   

                  Feel free to post the code for DataEditor.mxml

                   

                  -Alex

                  • 6. Re: Why is this handler called 3x?
                    JackHombre Level 1

                    There's only one editable column on this screen. I don't really know how to make it use the default editor properly. If I remove the itemEditor="DataEditor" then I just get crashes all over the place when editing finishes.

                    I wouldn't say DataEditor.mxml is a control, but here's the Flex from that file (I won't inundate you w/the AS):

                    <?xml version="1.0" encoding="utf-8"?>

                    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"

                        implements="mx.core.IDataRenderer, mx.controls.listClasses.IListItemRenderer"

                        xmlns:UBIComponent="assets.UBIComponent.*">

                    ...

                    <mx:FormItem visible="false" >
                    <UBIComponent:ubiComboBox id="cmbExplct" />
                    <UBIComponent:ubiComboBox id="cmbDateFormat" />
                             </mx:FormItem>

                    </mx:Canvas>

                    • 7. Re: Why is this handler called 3x?
                      Flex harUI Adobe Employee

                      That implies that there are multiple editable fields in the renderer.  This post may help: http://blogs.adobe.com/aharui/2008/08/datagrid_itemeditor_with_two_i.html