简体   繁体   中英

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.

For example, I return xml to component1 and component2 that differs in this way:

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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