Flex 中的一个关于Repeater组件的问题

	<mx:Tile direction="horizontal" borderStyle="inset">
		<mx:Repeater dataProvider="{imageList}" id="rp">
			<mx:Image source="{rp.currentItem.thumb}" click="{Alert.show(rp.currentItem.thumb)}"/>
		</mx:Repeater>
	</mx:Tile>

 

在做这个测试的例子的时候,发现会产生错误 ,后来仔细想想还是想明白了,在做click事件时,Repeater已经循环结束了, 这时候再去访问rp.currentItem则会产生问题。

 

那么把测试例子改成下面这样

<mx:script>
	function handle(event:MouseEvent){
		Alert.show(event.currentTarget.getRepeaterItem())
	}
</mx:script>

	<mx:Tile direction="horizontal" borderStyle="inset">
		<mx:Repeater dataProvider="{imageList}" id="rp">
			<mx:Image source="{rp.currentItem.thumb}" click="handle(event)"/>
		</mx:Repeater>
	</mx:Tile>

 那么关于event.currentTarget.getRepeaterItem()这个又是怎么回事呢,

event.currentTarget这个不用多讲了,这个例子是Image,那么getRepeaterItem()又不是Image的方法。GOOGLE一下,发现了以下说明

 

 

Repeated components and repeated Repeater components have a getRepeaterItem() method that returns the item in the dataProvider property that was used to produce the object. When the Repeater component finishes repeating, you can use the getRepeaterItem() method to determine what the event handler should do based on the currentItem property. To do so, you pass the event.currentTarget.getRepeaterItem() method to the event handler. The getRepeaterItem() method takes an optional index that specifies which Repeater components you want when nested Repeater components are present; the 0 index is the outermost Repeater component. If you do not specify the index argument, the innermost Repeater component is implied.

 

 那么可以看出,被Repeated的控制可以有一个方法,得到当时currentItem所对应的值。

你可能感兴趣的:(Google,Flex)