将 ClassFactory.generator 属性(来自 mxml)与其原始类进行比较
我在 MXML 标记中声明了一些组件,以及一些属性和事件处理程序。 对于其中一些需要重新实例化,所以我想出了以下架构。
将在某个时刻重新实例化的组件数组:
<flint:forms>
<fx:Component>
<login:LoginForm loginClick="outerDocument.form_loginClick()" />
</fx:Component>
<fx:Component>
<config:CustomizerWizard close="outerDocument.init()" />
</fx:Component>
</flint:forms>
在我的控制器中的某个时刻,当我需要一个全新的“LoginForm”时:
public var form:LoginForm;
...
form = ReDo(LoginForm);
ReDo 函数应该如下所示:
public function ReDo(classe:Class):* {
for each (var factory:ClassFactory in forms) {
if (factory.generator == classe) return factory.newInstance();
}
}
但它无法按照我希望的方式工作。 Flex 编译器为每个标记创建的类与其中声明的实际类不兼容。
我尝试创建自己的标签,但编译器不允许我以同样的方式使用它。 (抱怨它不是容器)
有没有办法使用自定义 ClassFactory 类和我可以在 ReDo 函数中使用的属性?像这样:
<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>
或者尝试将 ClassFactory.generator 属性转换/转换为可以预期的内容,而不是自定义 ClassFactory,而不是它现在拥有的随机类名?
编辑
我可以实例化列表中的每个组件并比较它是否是我想要的类型,而不是比较两个类变量(我认为这是最好的方法)。
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();
}
}
这将是我的代码中唯一的更改,但这不是浪费吗? GC 是否足够聪明,可以清理所有未通过“if”的 obj?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将事件捆绑并将处理程序移至更高的级别:
那么您基本上可以编写:
然后:
另一种(也是更正确的)方法是为每个组件使用单独的工厂函数:
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: