how to send Java object into between two mxml file

2019-09-12 06:15发布

问题:

I'm Using Flex + java .. in that i have two Sub mxml files Sub1.mxml and Sub2.mxml

Sub1.mxml code..

[Bindable] private var editTimetableVO:TimetableVO;
        public function forwardToCreateEvent(event:MouseEvent):void
        {
            editTimetableVO = new TimetableVO();
editTimetableVO=editDataGrid.selectedItem as TimetableVO;//editDataGrid is DataGrid id
                Alert.show("value   "+editTimetableVO.startDate);
            }

Hear Alert is Print some date it is good... then my Second Mxml file..

Sub2.mxml code..

public var myEditEvent:Sub1 = new Sub1();
private var timetableVO:TimetableVO = new TimetableVO();

    //  private var editTimetableVO:TimetableVO = new TimetableVO();
        protected function init(event:FlexEvent):void
        {
            Alert.show("Show");
            timetableVO=myEditEvent.editDataGrid.selectedItem as TimetableVO;

            Alert.show("value "+timetableVO.startDate);

        }

But in that time Alert not Printing .... Is their any other way to access to editTimetableVO in Sub1.mxml to Sub2.mxml file...

回答1:

package
{
public class ModelLocator{
    public static var instance:ModelLocator;

    public var editTimetableVO:*;

    public function ModelLocator(instance:SingletonEnforcer){}

    public static function getInstance():ModelLocator{
        if(!instance){
            instance = new ModelLocator(new SingletonEnforcer());
        }
        return instance;
    }
}

}class SingletonEnforcer{}

// sub1.mxml
[Bindable]private var model:ModelLocator = ModelLocator.getInstance();
    public function forwardToCreateEvent(event:MouseEvent):void
    {
        model.editTimetableVO = new TimetableVO();
        model.editTimetableVO=editDataGrid.selectedItem as     TimetableVO;//editDataGrid is DataGrid id
        Alert.show("value   "+model.editTimetableVO.startDate);
        }


// Sub2.mxml
[Bindable]private var model:ModelLocator = ModelLocator.getInstance();
    protected function init(event:FlexEvent):void
    {
        Alert.show("Show");
        model.timetableVO=myEditEvent.editDataGrid.selectedItem as TimetableVO;

        Alert.show("value "+model.timetableVO.startDate);

    }


回答2:

Try to create a modelLocator (singleton class and put reference of "editTimetableVO" in that file). This way only a single instance of "editTimetableVO" variable exist in whole application lifecycle, as you have declared this variable as Bindable so changes happen anywhere in application will reflect instantly.