When creating custom layouts in Flex, why do you "have to" override updateDisplayList() and measure()?

StackOverflow https://stackoverflow.com/questions/19168733

سؤال

When creating custom layouts in Flex, you generally override LayoutBase, in which case the documentation and tutorials on the Internet say you "have to" override the functions updateDisplayList() and measure(). . .Huh?

I don't think there is a construct in AS3 to syntactically require a function to be overridden, and there sure isn't one here. There's not even as special logic to raise unhandled Errors or anything. This works perfectly fine:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns="*" backgroundColor="#000000" showStatusBar="false" click="createImage()">
    <fx:Script>
        <![CDATA[
            import spark.components.Image;

            [Embed(source="AL.png")]
            private static const IMAGE:Class;

            private var iX:int = 0;
            private var iY:int = 0;

            private function createImage():void
            {
                var img:Image = new Image();
                img.source = IMAGE;
                img.width = 100;
                img.height = 100;

                img.x = iX
                img.y = iY;

                iX += 100;
                iY += 100;

                grp.addElement(img);
            }
        ]]>
    </fx:Script>
    <s:Group id="grp" width="400" height="400">
        <s:layout>
            <GridLayout/>
        </s:layout>
    </s:Group>
</s:WindowedApplication>

And in my GridLayout.as class:

package
{
    import spark.layouts.supportClasses.LayoutBase;

    public class GridLayout extends LayoutBase
    {
    }
}

Each time I click on the application, a new image is spawned at (x, y) * n, where n is the number of images previously spawned. It works perfectly fine. I even added code so that right-clicking would take an already existing image and move it around on the screen, and that worked fine as well.

So I guess my question is. . .Why in the world are they saying this is absolutely required? What do they mean? I can understand the documentation and stuff saying that those two functions are where you want to concentrate your customizations at or something like that, but in programming, saying that a function override is required is some pretty strong language; if stuff holds up just fine without you overriding those functions, why would both documentation and independent tutorials be saying this? Thanks!

هل كانت مفيدة؟

المحلول

Why in the world are they saying this is absolutely required?

Since I don't know what documentation you're reading; it is tough to say. The documentation could be wrong; or you could be taking something out of context. It is not required that you write any sort of code in an updateDisplayList() or measure() method, but...

What do they mean?

In the context of creating Flex components; it makes sense to make use of the Flex Component LifeCycle methods, for either MX components or Spark Components. The lifecycle methods (createChildren(), commitProperties(), updateDisplayList(), and measure() are executed by the Flex Framework and make use of an invalidation cycle built right into the framework.

The framework methods help you put "similar" code in the same place so you don't have it all over the place.

The Spark Framework changed things a bit by providing the option of separating layout and sizing code (AKA updateDisplayList() and measure() out of the main class and into a specific layout class.

saying that a function overload is required is some pretty strong language

I'd be surprised if you've read any docs that say that overloading is required. Overloading is the act of creating multiple functions with the same name, but different arguments. This is not supported in ActionScript. Overriding is the act of extending a function in a child component in order to change or extend functionality of that function. They are different concepts; and I'm unclear if that was a typo on your part or if you are confused about some tenants of object oriented programming.

Does this help? [Unfortunately, I don't have the time right now to try to re-work your sample to make proper use of Flex Framework conventions.

نصائح أخرى

 Why in the world are they saying this is absolutely required?

I understand your concern, these overriding functions are required for the components to works optimally. These components don't require to follow these component lifecycle principles if your applications is small, the component does not change frequently and no body else touches your class.

But, If your app needs to change these components frequently then you might have enforce these lifecycle principles.

For example: you want to Prepare Coke, steps(I'd say life cycle) would be take water > mix sugar > add colors > mix soda > bottle it

Now you get orders to change the color of the Coke that you just prepared, the life cycle is already completed, if you need to change anything at this point, you need to put a lot of effort even to change the color of it. If you had followed the life cycled, it makes life easier for the app at run time. No performance issues.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top