简体   繁体   English

变干/重构flex / actionscript代码

[英]Drying out/refactoring flex/actionscript code

I have something like 10 components that have code that's very similar, except for the target that it's applied to and some other minor differences. 我有10个类似的组件,它们的代码非常相似,除了所应用的目标和其他一些细微差别。

For example, I return xml to component1 and component2 that differs in this way: 例如,我将xml返回到component1component2 ,它们的区别在于:

component 1: event.result.names.name
component 2: event.result.phones.phone

I then bind these to a List , and the name of that list differs by component. 然后,将它们绑定到List ,该列表的名称因组件而异。

component 1: nameslist.dataProvider = 
component 2: phoneslist.dataProvider = 

How do I create a single method that would accept as parameters just the names/pointers/references to the objects I need worked on. 如何创建一个仅接受需要处理的对象的名称/指针/引用作为参数的方法。 For examples nameslist or phoneslist as List or .phones.phone vs. .names.name for the structure of the xml returned? 例如, nameslistphoneslist作为List.phones.phone.names.name作为返回的xml的结构?

It sounds to me like you can use a function as parameters to your component. 在我看来,您可以将函数用作组件的参数。

So, you'll have a property like this: 因此,您将拥有一个像这样的属性:

public var dataProviderFunction : Function;

and the value you'd give it might be something like this: 您提供的值可能是这样的:

public function getNamesDataProvider(object:XML):xml{ return xml.namesList.dataProvider }

Or like this: 或者像这样:

public function getNamesDataProvider(object:XML):xml{ return xml.phonesList.dataProvider }

Take a look at how the List class source code and see how the labelFunction and itemToLabel are implemented. 看一下List类源代码的用法,以及如何实现labelFunction和itemToLabel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM