I have some components declared in MXML tags, with some properties and event handlers.
For some of them there is the need to re-instantiated, so I came up with the following architecture.
Array of components that will be re-instanciated at some poin:
<flint:forms>
<fx:Component>
<login:LoginForm loginClick="outerDocument.form_loginClick()" />
</fx:Component>
<fx:Component>
<config:CustomizerWizard close="outerDocument.init()" />
</fx:Component>
</flint:forms>
At some point in my controller when I need a brand new “LoginForm”:
public var form:LoginForm;
...
form = ReDo(LoginForm);
The ReDo function should look like this:
public function ReDo(classe:Class):* {
for each (var factory:ClassFactory in forms) {
if (factory.generator == classe) return factory.newInstance();
}
}
But it does’t work as I would like it to. The class created by the Flex compiler for every tag its not compatible with the actual class that is declared in it.
I tried to create my own tag, but the compiler won’t let me use it in the same way that can. (Complains that it’s not a container)
Is there a way to use a custom ClassFactory class with a property that I could use in my ReDo function? Like this:
<flint:forms>
<flint:MyComponent type="loginForm">
<login:LoginForm loginClick="outerDocument.form_loginClick()" />
</flint:Component>
<flint:MyComponent type="wizard">
<config:CustomizerWizard close="outerDocument.init()" />
</flint:Component>
</flint:forms>
Or instead of the custom ClassFactory, try to cast/convert the ClassFactory.generator property to something that can be expected, not some random class name that it has righ now?
Edit
Instead of comparing both Class variables (which I think it’s the best aproach), I could instantiate every component in the list and compare if its the type that I want.
public function ReDo(classe:Class):* {
for each (var factory:ClassFactory in forms) {
var obj:* = factory.newInstance();
if (obj is classe) return obj;
//if (factory.generator == classe) return factory.newInstance();
}
}
That would be the only change in my code, but isn’t it wasteful? Would GC be smart enough to cleanup all the obj’s that don’t pass the “if”?
If you make events buddle and move handlers a level higher:
then you can write basically:
And then:
Another (and more correct) approach is using a separate factory function for each component: