Sunday, February 22, 2004

Inspectable Getters and Setters and Live Preview

I saw an interesting article over at Bit-101 about this subject.

Well its true, as they have found, the Live Preview in MX2004 is pretty screwed up (to use the correct technical term), leading to some convaluted and weird code trying to get things to work, we almost thought about knocking LivePreview on the head at one time!

The basic rule we have now settled on is that setters should never do anything except set an internal variable and then call invalidate(), nothing more... and you should never call draw() directly.

Regardless of how many times invalidate gets called in a frame it will only ever result in a single call to draw() on the NEXT frame, so the constructor, setters or whatever can fire as many times as they like in whatever order they like... and you just do your real work in the draw() function in the following frame.

This does away with the need to check for whether initialization is complete, and solves most of the problem cases.

For those complex situations that need more, there is also a boolean variable childrenCreated that tells you whether the initialisation has occured and you have passed through the creatChildren function. This can be used to allow you to abort functions when they are being called in the incorrect state.

We also decided in our own XPComponents (since were not using the MM framework we can do what we like) to add an initComplete() call to the end of construction process to help resolve one niggling problem, though I have noticed that in the last update to MX2004 they have now built in an _endInit() method which essentialy does the same thing. Except in an isolated case I am really sceptical of the need for this function.


There are a few other things that you should do that solve some other niggling issues that can drive you up the wall trying to resolve...

Place the getter first and put the metadata tag on the getter.

(Helps with getting the inspector to implicitly recognise non string props)

If you extend another class dont use the full path to the class you are extending, first import it using the full path and then extend using just the class name.
i.e
import com.mydomain.mycomp;
class newone extends mycomp{}

DONT do this
import com.mydomain.mycomp;
class newone extends com.mydomain.mycomp{}

(Helps with getting inherited props to be recognised by the inspector)

No comments:

Post a Comment