使用XML的Data Binding时一点注意

直接贴代码,下面的 绑定中 myBooks.book.price 这个参数在函数计算中没有用到,但是没有这个参数,price这个域的变化并不会触发函数的重新计算。

也许有人会跟我一样,想当然的觉得price变了也算整个myBooks变:)

xml 代码
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application   
  3.     xmlns:mx="http://www.adobe.com/2006/mxml"   
  4.     width="500" height="470"  
  5. >  
  6.   
  7.     <mx:Script>  
  8.         <![CDATA[         
  9.             [Bindable] 
  10.             private var myBooks:XML =  
  11.                 <books> 
  12.                     <book ISBN="1590595181"> 
  13.                         <title>Foundation ActionScript Animation: Making Things Move</title> 
  14.                         <author>Keith Peters</author> 
  15.                         <amazonUrl>http://tinyurl.com/npuxt</amazonUrl> 
  16.                         <price>1</price> 
  17.                     </book> 
  18.                     <book ISBN="1582346194"> 
  19.                         <title>Send in the Idiots: Stories from the Other Side of Autism</title> 
  20.                         <author>Kamran Nazeer</author> 
  21.                         <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl> 
  22.                         <price>2</price> 
  23.                     </book> 
  24.                 </books> 
  25.              
  26.             private function onClick():void 
  27.             { 
  28.                 myBooks.book.(@ISBN=="1590595181")[0].price = 10 
  29.             } 
  30.  
  31.             private function totalPrice(books:XML, price:String):int 
  32.             { 
  33.                 var total:int = 0; 
  34.                 for each(var item:XML in books.book) 
  35.                 { 
  36.                     total += Number(item.price); 
  37.                 } 
  38.                 return total 
  39.             } 
  40.         ]]>  
  41.     </mx:Script>  
  42.       
  43.     <mx:Panel   
  44.         title="Assigning XML data"  
  45.         paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"  
  46.     >  
  47.         <mx:Button label="Button" click="onClick()"/>  
  48.         <mx:Label text="{totalPrice(myBooks,myBooks.book.price)}"/>  
  49.         <mx:Label text="{myBooks.book[0].price}"/>  
  50.   
  51.     </mx:Panel>  
  52. </mx:Application>  

<mx:application><mx:panel><mx:button label="Button" click="onClick()"><mx:label text="{totalPrice(myBooks,myBooks.book.price)}"><mx:label text="{myBooks.book[0].price}">     </mx:label>
</mx:label>

</mx:button></mx:panel></mx:application>

你可能感兴趣的:(xml,Adobe,actionscript)