2009-03-06 6 views
1

Data binding in ActionScript ist wirklich cool. Aber was, wenn ich einen großen Schalter Refactoring will oder wenn Aussage in geschweiften Klammern in eine Funktion, zum Beispiel:wie Code in geschweiften Klammern in Flex Refaktor

 
{person.gender == 'male' ? 'Mr.' : 'Ms.'} 

in:

 
{salutation(person)} 

Der Compiler mich nicht läßt das tun. Ich weiß über Eigenschaften und ich könnte Getter und Setter auf das Objekt Person schreiben. Aber da ich jetzt inlined JSON-Objekte verwende, ist das nicht praktisch (denke ich). Was sind andere gute Möglichkeiten, diesen Code umzuformen?

Um Matts Kommentar zu beantworten. Der Datentyp einer Person ist einfach ein Objekt. Es wurde vom JSON-Format entschlüsselt, das von einem Serviceanruf stammt.

+0

Was der Datentyp für " Person"? –

Antwort

4

Sie müssen die Person-Klasse (vorausgesetzt, Sie haben eine) bindbar machen, damit dies funktioniert.

Da Sie jedoch sagen, dass Sie JSON-Objekte verwenden, nehme ich an, Sie haben nur anonyme Objekte, die aus einer JSON-Zeichenfolge analysiert wurden. In diesem Fall bin ich mir ziemlich sicher, dass das nicht funktionieren wird. Sie müssen ein stark typisiertes Objekt erstellen, das über bindbare Eigenschaften verfügt.

Gerade FYI: um zu vermeiden, benutzerdefinierten JSON-Parser zu schreiben, für jedes Objekt, das Sie erstellen möchten, können Sie stark typisierte Objekte aus Vanille-Objekte erstellen können einen bytearray Trick:

public static function toInstance(object:Object, clazz:Class):* { 
    var bytes:ByteArray = new ByteArray(); 
    bytes.objectEncoding = ObjectEncoding.AMF0; 

    // Find the objects and byetArray.writeObject them, adding in the 
    // class configuration variable name -- essentially, we're constructing 
    // and AMF packet here that contains the class information so that 
    // we can simplly byteArray.readObject the sucker for the translation 

    // Write out the bytes of the original object 
    var objBytes:ByteArray = new ByteArray(); 
    objBytes.objectEncoding = ObjectEncoding.AMF0; 
    objBytes.writeObject(object); 

    // Register all of the classes so they can be decoded via AMF 
    var typeInfo:XML = describeType(clazz); 
    var fullyQualifiedName:String = [email protected]().replace(/::/, "."); 
    registerClassAlias(fullyQualifiedName, clazz); 

    // Write the new object information starting with the class information 
    var len:int = fullyQualifiedName.length; 
    bytes.writeByte(0x10); // 0x10 is AMF0 for "typed object (class instance)" 
    bytes.writeUTF(fullyQualifiedName); 
    // After the class name is set up, write the rest of the object 
    bytes.writeBytes(objBytes, 1); 

    // Read in the object with the class property added and return that 
    bytes.position = 0; 

    // This generates some ReferenceErrors of the object being passed in 
    // has properties that aren't in the class instance, and generates TypeErrors 
    // when property values cannot be converted to correct values (such as false 
    // being the value, when it needs to be a Date instead). However, these 
    // errors are not thrown at runtime (and only appear in trace ouput when 
    // debugging), so a try/catch block isn't necessary. I'm not sure if this 
    // classifies as a bug or not... but I wanted to explain why if you debug 
    // you might seem some TypeError or ReferenceError items appear. 
    var result:* = bytes.readObject(); 
    return result; 
} 
+0

Wow! Ich habe seit meinen Delphi-RTTI-Tagen kein solches hässliches Hacking mehr gesehen. Cool ... –

+0

Krank, krank, hacken Christophe! Ich mag das. Ich könnte es einfach benutzen. – airportyh

Verwandte Themen