直接贴代码,下面的
绑定中
myBooks.book.price 这个参数在函数计算中没有用到,但是没有这个参数,price这个域的变化并不会触发函数的重新计算。
也许有人会跟我一样,想当然的觉得price变了也算整个myBooks变:)
<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>
也许有人会跟我一样,想当然的觉得price变了也算整个myBooks变:)
xml 代码
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application
- xmlns:mx="http://www.adobe.com/2006/mxml"
- width="500" height="470"
- >
- <mx:Script>
- <![CDATA[
- [Bindable]
- private var myBooks:XML =
- <books>
- <book ISBN="1590595181">
- <title>Foundation ActionScript Animation: Making Things Move</title>
- <author>Keith Peters</author>
- <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>
- <price>1</price>
- </book>
- <book ISBN="1582346194">
- <title>Send in the Idiots: Stories from the Other Side of Autism</title>
- <author>Kamran Nazeer</author>
- <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>
- <price>2</price>
- </book>
- </books>
- private function onClick():void
- {
- myBooks.book.(@ISBN=="1590595181")[0].price = 10
- }
- private function totalPrice(books:XML, price:String):int
- {
- var total:int = 0;
- for each(var item:XML in books.book)
- {
- total += Number(item.price);
- }
- return total
- }
- ]]>
- </mx:Script>
- <mx:Panel
- title="Assigning XML data"
- paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"
- >
- <mx:Button label="Button" click="onClick()"/>
- <mx:Label text="{totalPrice(myBooks,myBooks.book.price)}"/>
- <mx:Label text="{myBooks.book[0].price}"/>
- </mx:Panel>
- </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>