Hi Christoph,
I think one of the things you should look into doing, is try to avoid
directly referencing the items that are in your view in your code behind
class. By doing so this will allow you rebuild your view without changing
much to your base class. For example, say you have an application that has
a number of buttons that trigger some action. In your BaseClass.as you
could have a number of consts:
[Bindable] protected static const LABEL_SUBMIT:String = "Submit";
[Bindable] protected static const LABEL_RESET:String = "Reset";
Then a simple function to handle the processing of buttons:
protected function handleButtonClick(event:MouseEvent) {
var b:Button = event.targat as Button;
var label:String = b.label;
if(label == LABEL_SUBMIT) {
doSubmit();
}
else if(label == LABEL_RESET) {
doReset();
else {
throw(new Error("Invalid Label"));
}
}
Then in your mxml view you can just bind to the consts and make a call to
the generic function:
<mx:Button label="{LABEL_SUBMIT}" click="handleButtonClick(event)" />
<mx:Button label="{LABEL_RESET}" click="handleButtonClick(event)" />
This is an overly simplistic example, but it does demonstrate my point. As
Tom also pointed out, by separating out the view and the code, it looks much
cleaner and in turn is more maintainable. You'll also have code that is
more unit testable if you can just load in a class and not have to worry
about referencing an initialized view.
--Josh
On 9/19/07, Christoph Atteneder <[EMAIL PROTECTED]> wrote:
>
> What you should have done is declare a public Bindable in the code
> > behind AS
> > for each Button or whatever in the MXML file, where the variable name in
> > the
> > AS class matches the id parameter in the MXML.
> > The creationComplete() handler for the AS class then attaches all the
> > eventListeners.
>
>
> That´s exactly what I want to prevent with the second suggestion. In this
> inheritence usage I have to add manually ALL components on stage as a public
> property and set the id as attribute as you already mentioned.
>
> The point I´m not sure about is class extends mxml vs. mxml extends class.
> Shall the MXML extend the CodeBehind class as suggested in the examples,
> or the CodeBehind class extend the MXML.
>
> I hope this makes it clearer, what I´m not sure about.
>
> cheers,
>
> Christoph
>
> On 9/19/07, Tom Chiverton < [EMAIL PROTECTED]> wrote:
> >
> > > MyClassCodeBehind extends e.g. Canvas
> >
> > An AS class, yup.
> >
> > > in the MyClass.mxml the root node is type of MyClassCodeBehind. The
> > > mxml class extends more or less the class MyClassCodeBehind.
> >
> > Yeah, the MXML extends the AS.
> >
> > > In this case the class MyClassCodeBehind of course has no access to
> > > the components in the mxml file.
> >
> > What you should have done is declare a public Bindable in the code
> > behind AS
> > for each Button or whatever in the MXML file, where the variable name in
> > the
> > AS class matches the id parameter in the MXML.
> > The creationComplete() handler for the AS class then attaches all the
> > eventListeners.
> >
> > Have you seen
> > http://ricoonflex.wordpress.com/2007/07/05/apply-code-behind-to-components/
> > ?
> >
> > --
> > Tom Chiverton
> > Helping to confidentially leverage back-end architectures
> > on: http://thefalken.livejournal.com
> >
> > ****************************************************
> >
> > This email is sent for and on behalf of Halliwells LLP.
> >
> > Halliwells LLP is a limited liability partnership registered in England
> > and Wales under registered number OC307980 whose registered office address
> > is at St James's Court Brown Street Manchester M2 2JF. A list of members is
> > available for inspection at the registered office. Any reference to a
> > partner in relation to Halliwells LLP means a member of Halliwells LLP.
> > Regulated by the Law Society.
> >
> > CONFIDENTIALITY
> >
> > This email is intended only for the use of the addressee named above and
> > may be confidential or legally privileged. If you are not the addressee you
> > must not read it and must not use any information contained in nor copy it
> > nor inform any person other than Halliwells LLP or the addressee of its
> > existence or contents. If you have received this email in error please
> > delete it and notify Halliwells LLP IT Department on 0870 365 8008
> > .
> >
> > For more information about Halliwells LLP visit www.halliwells.com.
> >
> >
>
>