Building components by using code behind

 

Building components by using code behind

MXML and ActionScript languages have different own strengths and weaknesses for creating components:

  • When you create composite controls declaratively, MXML makes it easier to create and lay out child controls.
  • When you modify the behavior of components, that is, override their methods, you typically use ActionScript.

Most of the time, you use a combination of MXML and ActionScript when you build Flex components and applications. Earlier versions of Flex have provided several ways of using MXML and ActionScript together:

  • Placing ActionScript statements directly within MXML tags. This is used when defining inline event handlers.
  • Placing ActionScript statements within the <mx:Script> tag.
  • Including external ActionScript files by using the source property of the <mx:Script> tag.

Adobe® Flex™ 2 adds to these methods with a new technique for working with MXML and ActionScript called code behind.

With code behind, you use MXML code to layout your component and you place your ActionScript code in a class definition. To link the two together, you make the ActionScript class the root tag of your MXML component; that is, your MXML component extends the ActionScript class. For example, to create a custom AddressForm component that displays a composite address entry form, you do the following:

  1. Create an ActionScript class called AddressFormClass. You can make this class extend a base Flex class. In this case, you use the layout capabilities of the Form container and make AddressFormClass extend the mx.containers.Form class.
  2. Create an MXML component called AddressForm and make its root tag AddressFormClass.
  3. Use MXML to layout the contents of the AddressForm component.
  4. Use ActionScript to create the logic for the AddressForm component.

Tip: You must declare child controls as public properties in your ActionScript class.

The following example contains the custom AddressForm component described above. The main application file also makes use of code behind and the example also features the CountryComboBox and PaddedPanel components that you created in the other tutorials.

Link: Consider this as an introduction to best practices architecture when building Flex applications. For more information, see the Arp framework — an open source pattern-based framework for creating Flash and Flex applications that uses code behind.

Example

components/AddressFormClass.as

package components
{
    import mx.events.FlexEvent;
    import mx.controls.Button;
    import mx.controls.TextInput;
    import flash.events.MouseEvent;
    import mx.containers.Form;
    
    public class AddressFormClass extends Form
    {

        // Components in the MXML must be 
        // declared public. This is a limitation in 
        // the current version of Flex and may change
        // in the future. 
        public var submitButton:Button;
        public var nameInput:TextInput;
        public var street:TextInput;
        public var city:TextInput;
        public var state:TextInput;
        public var country:CountryComboBox;
        
        // Constructor

        public function AddressFormClass ():void
        {
            addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }

        
        // Creation complete is a good time to add event listeners and 
        // interact with child components.
        private function creationCompleteHandler (event:FlexEvent):void

        {
            submitButton.addEventListener(MouseEvent.CLICK, submitHandler);
        }
        
        // Gets called when the Submit button is clicked
        private function submitHandler (event:MouseEvent):void

        {
            // Gather the data for this form
            var addressVO:AddressVO = new AddressVO();
            addressVO.name = nameInput.text;
            addressVO.street = street.text;
            addressVO.city = city.text;
            addressVO.state = state.text;
            addressVO.country = country.selectedItem as String;
                        
            var submitEvent:AddressFormEvent = new AddressFormEvent(AddressFormEvent.SUBMIT);
            submitEvent.data = addressVO;
                        
            // Dispatch an event to signal that the form has been submitted 

            dispatchEvent(submitEvent);
        }
    }
}		

components/AddressForm.mxml

<?xml version="1.0" encoding="utf-8"?>
<custom:AddressFormClass 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:custom="components.*"

>
   <mx:FormItem label="Name">
        <mx:TextInput id="nameInput"/>
    </mx:FormItem>

    <mx:FormItem label="Street">
        <mx:TextInput id="street"/>
    </mx:FormItem>

    <mx:FormItem label="City"> 
        <mx:TextInput id="city"/>
    </mx:FormItem>

    <mx:FormItem label="State/County"> 
        <mx:TextInput id="state"/>
    </mx:FormItem>

    <mx:FormItem label="Country"> 
        <custom:CountryComboBox id="country"/>

    </mx:FormItem>
   
    <mx:Button 
        id="submitButton"
        label="Submit"
    />
</custom:AddressFormClass>	

components/AddressFormEvent.as

package components
{
    import flash.events.Event;
    import components.AddressVO;

    public class AddressFormEvent extends Event
    {

        public static const SUBMIT:String = "submit";

        private var _data:AddressVO;

        public function AddressFormEvent (eventName:String)

        {
            super (eventName);
        }
        
        public function set data (value:AddressVO):void

        {
            _data = value;
        }
        
        public function get data ():AddressVO
        {

            return _data;
        }
    }
}		

components/AddressVO.as

package components
{
    public class AddressVO
    {
        // We are using public properties for the
        // value object to keep this example short. In a

        // real-world application, make these properties
        // private and use implicit accessors to expose them
        // so you can do validation, etc. if necessary. 
        public var name:String;
        public var street:String;
        public var city:String;
        public var state:String;
        public var country:String;
    }

}
components/PaddedPanel.as
package components
{
    import mx.containers.Panel;

    public class PaddedPanel extends Panel
    {

        public function PaddedPanel()
        {
            // Call the constructor of the superclass.
            super();
            
            // Give the panel a uniform 10-pixel 

            // padding on all four sides.
            setStyle ("paddingLeft", 10);
            setStyle ("paddingRight", 10);
            setStyle ("paddingTop", 10);
            setStyle ("paddingBottom", 10);
        }

    }
}		

components/CountryComboBox.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:dataProvider>    
        <mx:String>United States</mx:String>
        <mx:String>United Kingdom</mx:String>

        <!-- Add all other countries. -->
    </mx:dataProvider>
</mx:ComboBo</mx:ComboBox>	
package components
{
    import mx.core.Application;
    import mx.events.FlexEvent;
    import mx.controls.Alert;
    
    import components.AddressFormEvent;
    import components.AddressVO;
    import flash.utils.describeType;
    
    public class ApplicationClass extends Application
    {

        // Components in MXML
        public var addressForm:AddressForm;
        
        public function ApplicationClass()

        {
            addEventListener (FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }
        
        //
        // Event handlers
        //

        private function creationCompleteHandler(event:FlexEvent):void
        {

            // The custom AddressForm component dispatches a "submit" 
            // event the form is submitted. Listen for this. 
            addressForm.addEventListener(AddressFormEvent.SUBMIT, submitHandler);
        }
        
        private function submitHandler(event:AddressFormEvent):void

        {
            // Get the value object (data) from the event object
            var data:AddressVO = event.data as AddressVO;
                                    
            // Compose a string to display the contents of the value object to the user.

            var msg:String = "You submitted the following information: /r";
            
            // Use the new introspection API and E4X to get a list of the properties
            // in the data object and enumerate over them to compose the string.
            var dataProperties:XMLList = describeType(data)..variable;
            
            for each (var i:XML in dataProperties) 
            {

                var propertyName:String = i.@name;
                msg += i.@name + ": " + data[i.@name] + "/r";
            }

            // Display the contents of the address form to the user.
            Alert.show(msg, "Thank you for submitting the form!");
        }
    }
}
Main application MXML file
<?xml version="1.0" encoding="utf-8"?>
<custom:ApplicationClass 
    xmlns:custom="components.*"

    viewSourceURL="src/CodeBehind/index.html"
    width="400" height="310"
>
    <custom:PaddedPanel title="Code Behind">

        <custom:AddressForm id="addressForm"/>
    </custom:PaddedPanel>
</custom:ApplicationClass>
Result
<script type="text/javascript"> // </script>

To view the full source, right-click the Flex application and select View Source from the context menu.


For more information

For more information, see "Creating Custom ActionScript Components" in Creating and Extending Flex 2 Components.

About the author

Aral Balkan acts and sings, leads development teams, designs user experiences, architects rich Internet applications, and runs OSFlash.org, the London Macromedia User Group, and his company, Ariaware. He loves talking design patterns and writing for books and magazines. He also authored Arp, the open-source RIA framework for the Flash platform. Aral is generally quite opinionated, animated, and passionate. He loves to smile, and can even chew gum and walk at the same time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于微信小程序的家政服务预约系统采用PHP语言和微信小程序技术,数据库采用Mysql,运行软件为微信开发者工具。本系统实现了管理员和客户、员工三个角色的功能。管理员的功能为客户管理、员工管理、家政服务管理、服务预约管理、员工风采管理、客户需求管理、接单管理等。客户的功能为查看家政服务进行预约和发布自己的需求以及管理预约信息和接单信息等。员工可以查看预约信息和进行接单。本系统实现了网上预约家政服务的流程化管理,可以帮助工作人员的管理工作和帮助客户查询家政服务的相关信息,改变了客户找家政服务的方式,提高了预约家政服务的效率。 本系统是针对网上预约家政服务开发的工作管理系统,包括到所有的工作内容。可以使网上预约家政服务的工作合理化和流程化。本系统包括手机端设计和电脑端设计,有界面和数据库。本系统的使用角色分为管理员和客户、员工三个身份。管理员可以管理系统里的所有信息。员工可以发布服务信息和查询客户的需求进行接单。客户可以发布需求和预约家政服务以及管理预约信息、接单信息。 本功能可以实现家政服务信息的查询和删除,管理员添加家政服务信息功能填写正确的信息就可以实现家政服务信息的添加,点击家政服务信息管理功能可以看到基于微信小程序的家政服务预约系统里所有家政服务的信息,在添加家政服务信息的界面里需要填写标题信息,当信息填写不正确就会造成家政服务信息添加失败。员工风采信息可以使客户更好的了解员工。员工风采信息管理的流程为,管理员点击员工风采信息管理功能,查看员工风采信息,点击员工风采信息添加功能,输入员工风采信息然后点击提交按钮就可以完成员工风采信息的添加。客户需求信息关系着客户的家政服务预约,管理员可以查询和修改客户需求信息,还可以查看客户需求的添加时间。接单信息属于本系统里的核心数据,管理员可以对接单的信息进行查询。本功能设计的目的可以使家政服务进行及时的安排。管理员可以查询员工信息,可以进行修改删除。 客户可以查看自己的预约和修改自己的资料并发布需求以及管理接单信息等。 在首页里可以看到管理员添加和管理的信息,客户可以在首页里进行家政服务的预约和公司介绍信息的了解。 员工可以查询客户需求进行接单以及管理家政服务信息和留言信息、收藏信息等。
数字社区解决方案是一套综合性的系统,旨在通过新基建实现社区的数字化转型,打通智慧城市建设的"最后一公里"。该方案以国家政策为背景,响应了国务院、公安部和中央政法会议的号召,强调了社会治安防控体系的建设以及社区治理创新的重要性。 该方案的建设标准由中央综治办牵头,采用"9+X"模式,通过信息采集、案(事)件流转等手段,实现五级信息中心的互联互通,提升综治工作的可预见性、精确性和高效性。然而,当前社区面临信息化管理手段不足、安全隐患、人员动向难以掌握和数据资源融合难等问题。 为了解决这些问题,数字社区建设目标提出了"通-治-服"的治理理念,通过街道社区、区政府、公安部门和居民的共同努力,实现社区的平安、幸福和便捷。建设思路围绕"3+N"模式,即人工智能、物联网和数据资源,结合态势感知、业务分析和指挥调度,构建起一个全面的数据支持系统。 数字社区的治理体系通过"一张图"实现社区内各维度的综合态势可视化,"一套表"进行业务分析,"一张网"完成指挥调度。这些工具共同提升了社区治理的智能化和效率。同时,数字社区还提供了包括智慧通行、智慧环保、居家养老和便民服务等在内的多样化数字服务,旨在提升居民的生活质量。 在硬件方面,数字社区拥有IOT物联网边缘网关盒子和AI边缘分析盒子,这些设备能够快速集成老旧小区的物联设备,实现传统摄像设备的智能化改造。平台优势体现在数字化能力中台和多样化的应用,支持云、边、端的协同工作,实现模块化集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值