The realization of AS3 event bubbling mechanism in Shanzhai version

Source: Internet
Author: User
Tags abstract continue

as 3 implements event passing, divided into three segments capture,target and bubble, where bubble is passed to the root of the tree. This mechanism is very classic and easy to use, unfortunately it only exists in the Displayobject object tree, other times can only be honest from one object directly to another object.

this time I'll play an interesting. using the built-in mechanism of as 3 to make a more general bubbling framework is believed to be useful (at least for my project). Implement event delivery yourself so that it supports any data structure (regardless of queue or tree) of any event, and can even pass any object (not limited to event objects). In fact, the process of bubble is not difficult, is to put an object (special case is the event object) in a chain of automatic transmission, continuous upward throw, process and AS3 event mechanism identical. to be able to expand, I prepared the interface and the initial implementation classes: Ibubbledispatcher and Cbubbledispatcher. It's the same as Ieventdispatcher and Eventdispatcher. Take a look at the code.

package org.gl.geom.abstract {Import flash.events.Event  /** * ... * @author Jonson * * Public interface ibubble Dispatcher {/** * add to Bubble tree * * @param child * @param father/function Registerasmychild
(child:ibubbledispatcher): void;
 /** * Dispatch event, then bubble * * @param event */function Bubble (event:event): void;
		  /** * it ' s better to is override in subclass * * @param event * @return true means continue bubble */function Mybubbleeventhandler (event:event): Boolean}  } 
package Org.gl.geom . Abstract {Import flash.events.Event  /** * ... * @author Jonson/public class Bubbledispatcher Implem Ents ibubbledispatcher {////////////////////////////////////////////////////render event bubble   Private V Ar __my_parent__:ibubbledispatcher;  /** * Add to Bubble tree * * @param child * @param father/Public function registerasmychild ( Child:ibubbledispatcher): void {if (bubbledispatcher) {(Child as bubbledispatcher). __my_parent__ = t
			His }  /** * Dispatch event, then bubble * * @param event */Public Function Bubble (event:event): VO
				ID {if (__my_parent__) {var neve:event = Event.clone ();
				if (__my_parent__.mybubbleeventhandler (neve)) {__my_parent__.bubble (neve); }}  /** * It's better to is override in subclass * * @param event * @return true means Contin UE Bubble/Public Function Mybubbleeventhandler (event:event): Boolean {//it ' s better to is override in Subclas
		s return true; }
	}
}

> for easy understanding, I'm here with AS3 The built-in eventdispatcher makes some comparisons.

  • make Cbubbledispatcher father Class (same as Eventdispatcher).
  • uses registerasmychild to register leaf objects (similar to the combination of Addchild and AddEventListener, first to take the leaf object as the child of the current object and then register to listen), so that The event objects thrown by the leaves can be processed by the parent object or thrown up. The
  • uses bubble to emit events (equivalent to dispatchevent). The
  • subclass overrides Mybubbleeventhandler to handle the event, and returns a true description to continue passing up (similar to the Stoppropagation feature) and does not need to be rewritten if no processing is required. The advantage of this design is that there is no need to write a lot of AddEventListener according to the different event type.

Complement small example

class Parent1class extends Cbubbledispatcher {public function
      Parent1class () {this.registerasmychild (_child);}
private var _child:parent2class = new Parent2class ();
      Class Parent2class extends Cbubbledispatcher {public Function Parent2class () {this.registerasmychild (_child);}
private var _child:childclass = new ChildClass (); Class ChildClass extends Cbubbledispatcher {public Function try (): Void{this.bubble (The new Event ("Up")};} 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.