Implement undo undo in flex

How to implement cancel cancel operation in flex 4 to save history? I work with flex UIComponent and DisplayObjects to create and edit diagrams, but in flex there is no way to directly process the history of user operations. is there any idea to achieve this?

+5
source share
3 answers

You can implement the Command Template for all actions using the execute and undo methods and place them in the queue.

therefore, when the user wants to do something - say, create the AddFiveToTotal class and execute:

public method execute():void{
     totalModel.add( 5 );
}

This class will then be stored in the FIFO queue.

if the user must cancel the command, it will slip out, and the cancel function will be called:

public method undo():void{
     totalModel.subtract( 5 );
}

, pop,

Memento Pattern

- MVC (S), , , ,

+5

. -, Vector ( , , ), , Vector . /, , , , /Redos.

The second approach is to save only what has changed in this vector, so you do not have to create and clone a lot of objects, but save only one object in Vector, which will contain all the properties that have changed and their last values. This will give you something like this:

private var mHistory:Vector.<Object> = new Vector.<Object>();
private var mCurrentIndex:int = -1;

public function storeState(state:Object)
{
   mHistory.push(state);
   mCurrentIndex++;
}     

public function undo():void
{
   if(mCurrentIndex < 1)
       return;

   mCurrentIndex--;

   //here you could test for values that could have changed
   var item:DisplayObject = this.getChildByName(mHistory[mCurrentIndex].name);
   if(mHistory[mCurrentIndex].x != undefined)
       item.x = mHistory[mCurrentIndex].x;
   // etc. you get the point. Note that this is only comfortable if only several things can change
}

And the function call storeStatewill be like this:

var state:Object = { name:DisplayObjectName, x:120, y:20 };
storeState(state);

Again, you will need to listen to all movements and changes if you want to record them.

0
source

All Articles