1

I have a protected function defined in a script block of a component defined in mxml like so:

<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="onCreationComplete()">
    <fx:Script>
    <![CDATA[
    protected function onCreationComplete():void {

    }
    ...

Is it possible to override the function in an instance of the component like so:

<gen:CreateObjectFormSubmit id="formSubmit">
     <fx:Script>
     <![CDATA[
          override protected function onCreationComplete():void {
              form=form1;
          }
     ]]>
     </fx:Script>
1
  • your sample should work perfect! what is the problem ? Commented Apr 4, 2012 at 8:42

3 Answers 3

4

Sure, you can. MXML component is a class , so if you create component B based on component A (B inherits from A), then you can override methods of A in B.

Sign up to request clarification or add additional context in comments.

Comments

1

Just be careful with where and how you are declaring your super class. From the syntax provided above, it appears you might be trying to declare a <component> tag in your mxml class, in which if this is the case - the compiler treats this as a separate child object and not an actual extension.

For one, root level tags cannot declare an id (but components can - and this becomes the Class name), and you would most likely need to explicitly declare all your xml namespaces.

In order for it to be a true extension, your first <VGroup> Class would need to be named CreateObjectFormSubmit, this becomes the root tag of your extending class. There is one gotcha though, if you extend from an mxml class you cannot declare any additional children (in mxml notation because of layout rules).

If as you say an instance of your class, then no you wouldn't be able to override it, since the function scope of the <Script> tag would still reside within the root level. This is also sometimes referred to as the 'outerDocument' when declaring <component> tags.

The above wouldn't be any different doing it in regular AS like the following:

class SomethingCool extends UIComponent {
    ...
    public function addButtons():void
    {
         var btn:Button = new Button();
             btn.id = 'formSubmit';
             //can't declare an override of Button here
    }
}

1 Comment

This is what I meant by this question, I dunno why I thought there would be anonymous class extension in Flex.
1

to override any function, a function which u want to override should be available in parent class of ur current class.

public class A {
  public function methodtooverride():void{
    trace('in class A');
  }
}

public class B extends A {
  override public function methodtooverride():void{
    trace('in class B');
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.