Flex Event stopimmediatepropagation VS stoppropagation

Categories: Flex; Tagged with: ; @ June 17th, 2011 17:35

What’s the difference ? Here is the test codes:

We will create a button in a Group, and listen the button click event(MouseEvent.Click):

		protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void {
			//  Create button and container
			var hbox:HGroup = new HGroup();
			var button:Button = new Button();
			button.label = "Button";

			// Add Eventer litener
			button.addEventListener(MouseEvent.CLICK, onButtonClick1);
			button.addEventListener(MouseEvent.CLICK, onButtonClick2);
			hbox.addEventListener(MouseEvent.CLICK, onButtonClick3);

			hbox.addElement(button);
			addElement(hbox);
		}

			// Button click handler 1
			private function onButtonClick1(e:MouseEvent):void {
				e.stopImmediatePropagation(); // or e.stopPropagation();
				trace("Handler 1");
			}

			// Button click handler 2
			private function onButtonClick2(e:MouseEvent):void {
				trace("Handler 2");
			}

			// Button click handler 3 listened by hbox
			private function onButtonClick3(e:MouseEvent):void {
				trace("Handler 3");
			}

Here is the difference:

when use stop propagation in the handler 1st, the trace is:

Handler 1
Handler 2

but when use stopImmediatePropagation(), the trace is:

Handler 1

That’s pretty easy to find the differences:

“The stopImmediatePropagation() method also prevents the Event objects from moving on to the next node, but it does not allow any other event listeners on the current node to execute.” (Adobe livedocs)

Flex 自定义Event 低级错误一例: 未重写Clone方法, 导致类型转换失败

Categories: Flex; Tagged with: ; @ December 10th, 2010 13:59

在Event监听并转发过程中, 因为没用重写Clone方法, 导致实例被Clone为普通Event实例.

情况是这样的:
自定义Event: EventTest: 继承Event, 增加了一些属性, 重写了toString(), 未重写Clone.

出现问题: 创建EventTest实例后, 多次监听并转发, 在监听, 转发过程中, Event实例对象被Clone, 因为未重写Clone方法, 会通过Event类的Clone方法进行复制, 仅会复制其EventType, 且类型为Event.

形象的说就是: eventTest –> 被监听到, 准发 ->框架Clone eventTest(EventTest类中未重写Clone方法, 于是eventTest被克隆为一个普通Event) –> 再次监听, Event类型不吻合, 报错.

公司代码规范中严格规定了自定义Event必须及时重写Clone与toString方法, 写了这么久, 我才领悟到原因. 败了

Flex中使用currentStateChange事件监听State变化

Categories: Flex; Tagged with: ; @ October 9th, 2010 0:12

StateChangeEvent.CURRENT_STATE_CHANGE(currentStateChange):
StateChangeEvent 类代表当组件的 currentState 属性更改时调度的事件。

中文API: http://www.adobe.com/livedocs/flex/3_cn/mx/events/StateChangeEvent.html

使用方法:

1. ActionScript中监听Event

ui.addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE, onCurrentStateChange);

注意: 如果用在主程序Application creationComplete的响应方法中, 可能无法监听首个StateChange事件(进入初始State);

2. MXML中监听Event

currentStateChange="onCurrentStateChange(event)"

附录: 响应函数

/** On State Change . */
protected function onCurrentStateChange(event:StateChangeEvent):void {
	printLog("LastState: " + event.oldState + "; currentState" + currentState);
}

Trace信息:

LastState: StateEmployeesList; currentStateStateDeptMgt

关于FlexBuilder的Event注释

Categories: Flex; Tagged with: ; @ March 15th, 2009 10:28

如果某个类派发某事件, 我们通常使用如下注释:

/** * Dispatched when …. * @eventType com.events.MyEvent.EVENT_TEST */
[Event(name=”myEvent” type=”com.events.MyEvent”)]

 

在使用new testr().addEvent…时提示的Const是错误的.MyEvent.MY_EVENT

解决方法:

[Event(name=”eventTest” type=”com.events.myevent”)]
此时的提醒为MyEvent.EVENT_TEST
[]中的name应与Event中的const的名字 如EVENT_TEST:String = "myEvent"; 中的EVENT_TEST对应 ,’_"后的字母保留大写去掉下划线->eventTest即可.

 

注意: Flex框架中的事件, 其Const 与String的name都是想对应的.

==>不得不承认 这样子比较挫<==

所以在编写自定义事件时, 最好也遵守这个不成文的规定:

public static const EVENT_EO_EDITOR:String = "eventEoEditor";

而不是随便乱写一个String, 这样便于在其他派发该事件的类中编写事件注释.

Flex中addEventListener失败原因总结

Categories: Flex; Tagged with: ; @ October 9th, 2008 2:01

我绝对是个粗心大意的人, 写代码90%的时间是浪费在低级错误上. 今天不幸又add错一把, 索性将犯错原因归纳如下:

1.弱智级别:*****  隔着锅台上炕:  没有dispatch就addEventListener

2.弱智级别:****   驴唇不对马嘴:  addEventListener中Event类型不符
3.弱智级别:***    驴唇不对马嘴:   Listener参数中Event类型不符[FleBuilder会在编译时报错]
4.弱智级别**      脑子让驴踢了:   在addEventListener之前就dispatch Event[不太容易发现]

自定义事件常见错误:

1. Type不使用Const的String

2. 构造函数中super(type…)中type填写错误.

Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.