Apache flex 如何在flex中重构大括号内的代码

Apache flex 如何在flex中重构大括号内的代码,apache-flex,actionscript,Apache Flex,Actionscript,真的很酷。但是,如果我想将大括号内的一个大开关或if语句重构为一个函数,例如: {person.gender == 'male' ? 'Mr.' : 'Ms.'} {person.gender=='male'?'Mr.:'Ms.} 进入: {称呼(人)} 编译器不允许我这么做。我知道属性,可以在person对象上编写getter和setter。但由于我现在使用的是内联JSON对象,所以这并不方便(我认为)。重构此代码的其他好方法有哪些 回答马特的评论。person的数据类型只是普通对象。它是从服

真的很酷。但是,如果我想将大括号内的一个大开关或if语句重构为一个函数,例如:

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

{称呼(人)} 编译器不允许我这么做。我知道属性,可以在person对象上编写getter和setter。但由于我现在使用的是内联JSON对象,所以这并不方便(我认为)。重构此代码的其他好方法有哪些


回答马特的评论。person的数据类型只是普通对象。它是从服务调用的JSON格式解码而来的。

您需要使Person类(假设您有一个)可绑定才能工作

但是,既然您说您使用的是JSON对象,那么我假设您只有从JSON字符串解析的匿名对象。那样的话,我敢肯定那是行不通的。您需要创建一个具有可绑定属性的强类型对象

仅供参考:为了避免为每个要创建的对象编写自定义JSON解析器,您可以使用bytearray技巧从普通对象创建强类型对象:

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 = typeInfo.@name.toString().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;
}

您需要使Person类(假设您有一个)可绑定,以使其工作

但是,既然您说您使用的是JSON对象,那么我假设您只有从JSON字符串解析的匿名对象。那样的话,我敢肯定那是行不通的。您需要创建一个具有可绑定属性的强类型对象

仅供参考:为了避免为每个要创建的对象编写自定义JSON解析器,您可以使用bytearray技巧从普通对象创建强类型对象:

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 = typeInfo.@name.toString().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;
}

“person”的数据类型是什么?person的数据类型是什么?哇!自从我的Delphi RTTI时代以来,我从未见过这种讨厌的黑客行为。酷…恶心,恶心,哈克·克里斯托夫!我喜欢。我可能会用的。哇!自从我的Delphi RTTI时代以来,我从未见过这种讨厌的黑客行为。酷…恶心,恶心,哈克·克里斯托夫!我喜欢。我可能会用它。