Flex callback function

Source: Internet
Author: User

What is a callback function? What is the function?

A callback function is a function called by a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to which it points, we will say this is a callback function. A callback function is called by another method when a specific event or condition occurs rather than by the implementation method of the function. It is used to correspond to the event or condition.

The process of implementing the callback function:

1. Define a callback function;

2. The party that provides function implementation registers the function pointer of the callback function to the caller during initialization;

3. When a specific event or condition occurs, the caller uses the function pointer to call the callback function to process the event.

The callback function separates callers from callers. Therefore, callers do not care who are called. It only needs to know that there is a called function with a specific prototype and restrictions.

As in the previously written event mechanism, you can use the button. addeventlistener (mouseevent. click, buttonclickhandler) to listen to events. Here is an application of the callback function. We first define a buttonclickhandler method, and then register the buttonclickhandler function reference to the addeventlistener method during initialization, when mouseevent. when a click occurs, the buttonclickhandler method written by callback is referenced through this function. This process is perfect for the callback function.

The following code is used to implement a callback function application. Previously, flex event mechanism (ii) used events to transmit data between parent and child components, in this article, the callback function is used to implement the callback function.

Main. mxml

<? XML version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "xmlns: components =" components. * "creationcomplete =" creationcompletehandler (event) "> <FX: SCRIPT> <! [CDATA [import MX. events. flexevent; // 2. during initialization, register the callback function pointer to the caller protected function creationcompletehandler (Event: flexevent): void {component. callbackfunction = showdata;} // 1. define a callback function private function showdata (data: string): void {textarea. TEXT = data ;}]]> </FX: SCRIPT> <s: layout> <s: verticallayout/> </S: layout> <s: textarea id = "textarea"/> <components: component1 id = "component"/> </S: Application>

Child component File Components/component1.mxml

<? XML version = "1.0" encoding = "UTF-8"?> <S: group xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx"> <FX: SCRIPT> <! [CDATA [Public var callbackfunction: function; // 3. When the button is clicked, use the function pointer to call the callback function to process the event. Protected function buttonclickhandler (Event: mouseevent): void {// The first parameter is the object where callbackfunction is located, and the second parameter is the callbackfunction parameter required by callbackfunction. call (this. parent, "Haha") ;}]]> </FX: SCRIPT> <s: button label = "display content" Click = "buttonclickhandler (Event) "/> </S: group>

The basic application of the Flex callback function is complete. It is very simple. You are welcome to give your comments on my blog post, because one of the projects involved in this time is mainly flex development, so there are a lot of flex summary, and they are also relatively beginner. Thank you for your support.

This article is from: Gao Shuang | Java and flex Corder. Original address: http://blog.csdn.net/ghsau/article/details/7400556.

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.