Flex Datagrid 中的选择不会将 valueObject 传递给 selectionChangeHandler 函数

Flex Datagrid 中的选择不会将 valueObject 传递给 selectionChangeHandler 函数

问题描述:

我有一个 TabNavigator,每个选项卡都是一个模块.其中一个模块被标记为 Units,该模块的完整代码发布在这篇文章中.

I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.

有几个问题:1) 表单未填充来自数据网格选择的数据.2) 选择一行并单击删除会出现非常常见的错误:TypeError: Error #1009: 无法访问空对象引用的属性或方法.selectionChangeHandler 函数内的 valueObject 单元的跟踪给出 NULL.为什么?

There are several problems: 1) Forms are not populated with data from the datagrid selection. 2) Selecting a row and clicking delete gives the very-common error: TypeError: Error #1009: Cannot access a property or method of a null object reference. A trace on the valueObject unit within the selectionChangeHandler function gives NULL. Why?

注意:在其他模块(TabNavigator 的其他选项卡)中,我在 DropDownLists 中填充了单位.这意味着 valueObject 单元是在其他模块中定义的.但是, valueObjects 应该是模块私有的,而不是共享的.我不确定问题出在哪里.

Note: In other modules (other tabs of the TabNavigator), I have DropDownLists populated with units. This means that the valueObject Unit is defined in the other modules. However, valueObjects should be private to modules, and not shared. I am unsure where the problem comes.

完整模块代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          xmlns:unitservice="services.unitservice.*"
          xmlns:valueObjects="valueObjects.*"
          width="724"
          height="674">
    <fx:Style source="assets/CAaNDFlex.css"/>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.rpc.events.ResultEvent;

            import spark.events.GridSelectionEvent;

            protected function unitsDg_creationCompleteHandler(event:FlexEvent):void
            {
                getUnitsResult.token=unitservice.getUnits();
            }

            protected function addBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsAdd";
                unit=new Unit();
            }

            protected function unitsDg_selectionChangeHandler(event:GridSelectionEvent):void
            {
                trace(event.currentTarget.selectedItem); //Unit object detected
                trace(event.currentTarget.selectedItem as Unit); //NULL 
                trace(unit); // unit is NULL. Why?
                currentState="unitsDetails";
            }


            protected function button_clickHandler(event:MouseEvent):void
            {
                trace(unit); // unit is NULL. Why?
                unit.unitName=unitNameTextInput.text;
                if (unit.unitID == 0)
                {
                    createUnitResult.token=unitservice.createUnit(unit);
                }
                else
                {
                    updateUnitResult.token=unitservice.updateUnit(unit);
                }
            }

            protected function updateBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsUpdate";
            }

            protected function createUnitResult_resultHandler(event:ResultEvent):void
            {
                currentState="unitsDetails";
                unit.unitID=event.result as int;
                unitsDg.dataProvider.addItem(unit);
                unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(unit));
                unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
            }

            protected function deleteBtn_clickHandler(event:MouseEvent):void
            {
                deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);

            }

            protected function deleteUnitResult_resultHandler(event:ResultEvent):void
            {
                unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
                currentState="units";
            }

        ]]>
    </fx:Script>
    <s:states>
        <s:State name="units"/>
        <s:State name="unitsDetails"/>
        <s:State name="unitsAdd"/>
        <s:State name="unitsUpdate"/>
    </s:states>
    <fx:Declarations>
        <s:CallResponder id="getUnitsResult"
                         result="unit = getUnitsResult.lastResult as Unit"/>
        <unitservice:UnitService id="unitservice"
                                           fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                           showBusyCursor="true"/>
        <valueObjects:Unit id="unit" />
        <s:CallResponder id="createUnitResult"
                         result="createUnitResult_resultHandler(event)"/>
        <s:CallResponder id="updateUnitResult"/>
        <s:CallResponder id="deleteUnitResult" result="deleteUnitResult_resultHandler(event)"/>



        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Binding destination="unit" source="unitsDg.selectedItem as Unit"/>

    <s:DataGrid id="unitsDg" x="10" y="37"
                creationComplete="unitsDg_creationCompleteHandler(event)" requestedRowCount="4"
                selectionChange="unitsDg_selectionChangeHandler(event)">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="unitName"
                              headerText="unitName">
                </s:GridColumn>
                <s:GridColumn dataField="unitID"
                              headerText="unitID">
                </s:GridColumn>
            </s:ArrayList>
        </s:columns>
        <s:typicalItem>
            <fx:Object unitID="unitID1"
                       unitName="unitName1">
            </fx:Object>
        </s:typicalItem>
        <s:AsyncListView list="{getUnitsResult.lastResult}"/>
    </s:DataGrid>
    <s:Button id="addBtn" x="10" y="0" label="Add" click="addBtn_clickHandler(event)"
              styleName="actionButton"/>
    <s:Form includeIn="unitsAdd,unitsUpdate"
            x="10"
            y="176"
            defaultButton="{button}">
        <s:FormItem label="unitName">
            <s:TextInput id="unitNameTextInput"
                         text="{unit.unitName}"/>
        </s:FormItem>
        <s:Button id="button"
                  label="Add"
                  click="button_clickHandler(event)"
                  label.unitsUpdate="Update"/>
    </s:Form>
    <s:Button id="updateBtn" x="138" y="0" label="Update" click="updateBtn_clickHandler(event)"/>
    <s:Button id="deleteBtn" x="266" y="0" label="Delete" click="deleteBtn_clickHandler(event)"/>
    <s:Form includeIn="unitsDetails" x="10" y="176">
        <s:FormItem label="unitName">
            <s:Label id="unitNameLabel" text="{unit.unitName}"/>
        </s:FormItem>
    </s:Form>
</s:Module>

selectedObject 未成功转换为 Unit,这意味着它可能不是转换之前的 Unit 或子类.将它投射到 Unit 不会使其成为 Unit,除非它之前是 Unit.

The selectedObject is not successfully casting to Unit, which means it probably wasn't Unit or a subclass prior to the cast. Casting it to Unit won't make it a Unit unless it was one before.