0

所以我得到了这个基于 TitleWindow 的 Flex 应用程序,其中这些窗口由其中编写的静态函数调用。这是从 DataGrid 创建或编辑实体需要时的样子:

private function incluir():void {
    NavioForm.incluir(dg.dataProvider);
}

private function atualizar():void {
    NavioForm.atualizar(dg.dataProvider, dg.selectedIndex);
}

从这方面来看,它工作得很好。

但是由于我使用了静态函数,代码开始变得有点重复,正如我们在下面的示例中看到的那样:

[CRUD 表单的脚本标签(incluir == include, atualizar == update, excluir == delete)]

...

[Bindable] private var navio:Navio;

public static function incluir(dataList:IList):void {
    var form:NavioForm = new NavioForm();
    form.action = FormWindow.ACTION_NEW + Navio.name;

    form.navio = new Navio();
    form.navio.lastUpdate = new Date();

    form.result = function():void {
        PortoService.obj.persistirNavio(form.navio).result(function(navio:Navio):void {
            dataList.addItem(navio);
            form.close();
        }).fault(function(event:FaultEvent):void {
            if(event.fault.faultString == 'duplicate key') {
                Util.showError("This vessel's IMO is already present in our database.");
            } else throw event.fault;
        });
    };

    PopUp.add(form);
}

public static function atualizar(dataList:IList, index:int):void {
    var form:NavioForm = new NavioForm();
    form.action = FormWindow.ACTION_UPDATE + Navio.name;
    form.imoRecieved = true;

    form.navio = dataList[index];

    PortoService.obj.obter(Navio, form.navio.key).result(function(navio:Navio):void {
        form.navio = navio;
        form.navio.lastUpdate = new Date();
    });

    form.result = function():void {
        PortoService.obj.persistir(form.navio).result(function(navio:Navio):void {
            dataList[index] = navio;
            form.close();
        }).fault(function(event:FaultEvent):void {
            if(event.fault.faultString == 'duplicate key') {
                Util.showError("This vessel's IMO is already present in our database.");
            } else throw event.fault;
        });
    };

    PopUp.add(form);
}

...

另一个 CRUD 表单的脚本标签:

...
[Bindable] private var vesselType:VesselType;

public static function incluir(dataList:IList):void {
    var form:VesselTypeForm = new VesselTypeForm();
    form.action = FormWindow.ACTION_NEW + VesselType.name;

    form.vesselType = new VesselType();

    form.result = function():void {
        CoreService.obj.persistir(form.vesselType).result(function(type:VesselType):void {
            dataList.addItem(type);
            form.close();
        });
    };

    PopUp.add(form);
}

public static function atualizar(dataList:IList, index:int):void {
    var form:VesselTypeForm = new VesselTypeForm();
    form.action = FormWindow.ACTION_UPDATE + VesselType.name;

    form.vesselType = Util.clone(dataList[index]);

    form.result = function():void {
        CoreService.obj.persistir(form.vesselType).result(function(type:VesselType):void {
            dataList[index] = type;
            form.close();
        });
    };
    form.deleteClick = function():void {
        CoreService.obj.excluir(form.vesselType.key).result(function():void {
            dataList.removeItemAt(index);
            form.close();
        });
    };

    PopUp.add(form);
}

那么,是否有设计模式或任何其他技术来完成这项工作?

4

1 回答 1

0

您可以创建一个 crud 组件,使用所有动态内容(例如数据提供者位置)进行实例化,并广播您分配适当侦听器的事件(或信号)。

于 2013-01-09T05:35:34.113 回答