flex类的双向绑定

1)官方的使用方法:使用[bindable]和@前缀.

2)报错:

class is not an IEventDispatche

解决如下:

function resultHandler(result:Array) {
   for(var i:String in result) result[i] = new ObjectProxy(result[i]);
   targetArrayCollection = new ArrayCollection(result);
}

 

产生原因如下,大意是使用AMF远程访问时会产生此问题。

This problem occurs frequently when transferring complex objects using AMF
when the object returned from the server contains an array or array
collection of more objects.
 
One symptom of this problem is that it occurs after converting the data
request from an HTTPService call to a Remote Object call.

It turns out that Flex does not handle data returned from the AMF data
service in quite the same way as HTTPService and WebService results. With
these two later services Flex will automatically wrap Arrays in
ArrayCollection and Objects in ObjectProxy wrappers so binding will work. 

The solution is to do this same thing manually, with code similar to the
following:
 

 

3)自定义双向绑定,出自一日本人:

   1. package cn.sloppy
   2. {
   3.     import flash.events.Event;
   4.     import mx.binding.utils.ChangeWatcher;
   5.     public class TwoWayBinding
   6.     {
   7.         public static function create(src1:Object, prop1:String, src2:Object, prop2:String):void
   8.         {
   9.             var flag:Boolean = false;
  10.  
  11.             ChangeWatcher.watch(src1, prop1, function(event:Event):void
  12.             {
  13.                 if(!flag)
  14.                 {
  15.                     flag = true;
  16.                     src2[prop2] = src1[prop1];
  17.                     flag = false;
  18.                 }
  19.             });
  20.  
  21.             ChangeWatcher.watch(src2, prop2, function(event:Event):void
  22.             {
  23.                 if(!flag)
  24.                 {
  25.                     flag = true;
  26.                     src1[prop1] = src2[prop2];
  27.                     flag = false;
  28.                 }
  29.             });
  30.         }
  31.     }
  32. }

 

调用如下:

   1. <?xml version="1.0" encoding="utf-8"?>
   2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute">
   3.     <mx:Script>
   4.         <![CDATA[
   5.             import cn.sloppy.User;
   6.             import mx.binding.utils.ChangeWatcher;
   7.             import mx.controls.Alert;
   8.             import cn.sloppy.TwoWayBinding;
   9.             [Bindable]
  10.             private var user:User;
  11.             private function init():void{
  12.                 user = new User();
  13.                 TwoWayBinding.create(myText,"text",user,"name");
  14.             }
  15.             private function showUserNameHandler():void{
  16.                 Alert.show(user.name);
  17.             }
  18.         ]]>
  19.     </mx:Script>
  20.     <mx:TextInput  text="{user.name}" id="myText" x="118" y="31">
  21.        
  22.     </mx:TextInput>
  23.     <mx:Button x="118" y="77" click="showUserNameHandler()" label="Button"/>
  24. </mx:Application>

 

你可能感兴趣的:(xml,webservice,Flex,Flash,Adobe)