Code Behind

  • Code behind is MXML and ActionScript but combined in a new way to use the strengths of both
  • The logic is written in ActionScript and is placed in an ActionScript class definition
  • The layout is coded in an MXML component
  • They are linked together making the ActionScript class the root of the MXML component
  • Make sure any child control declarations that are in the ActionScript class are public

previous page next page

Your Rating:
Results:
PatheticBadOKGoodOutstanding!
0 rates

ComboBoxCodeBehind.as

CodeBehindDisplay.mxml

CodeExample.mxml

  • Download aZIP filethat contains the applications in this module. To import the project into Flex Builder, select File > Import Flex Project.

Rendered Example

previous page next page

  1. Jul 31, 2008

    A few corrections to this example:

    1.  In CodeExample.mxml, <local:CodeBehindForm /> should say <local:CodeBehindDisplay />

    2.  In Quan Li's solution above, if a user selects day '31' from a month with 31 days and then changes the month dropdown to a month with only 30 days, '31' will still be incorrectly shown in the day dropdown.  An easier fix is to briefly change the day index to force a refresh on the displayed value:

            private function monthsHandler(event:ListEvent):void{
                setDays(event.currentTarget.selectedIndex);
                selectedIndexDay = 1;
                selectedIndexDay = 0;
            }

    3.  A more subtle logic error in this example involves the month dropdown.  Change your computer date to 31 July 2008.  Then refresh this page and notice the month dropdown has no February month and two March months.  This is because 'now' is a date with 31 days but when now.setMonth(i) is called in the init() function, setMonth tries to set the date to February 31st (which doesn't exist) so 'now' is converted to February 29th + 2 days which equals March 2nd so 'March' appears in the dropdown for February.  To fix this, in the init() function, set 'now' to be the first day of the month:

                for (i=0; i<12; i++){
                    now.setDate(1);
                    now.setMonth(i);
                    months[i] = dateformatter.format(now);
                } 

    Subtle bug, eh?

  2. Jul 30, 2008

    One more minor point: the previous tutorial, "Custom Components - ActionScript 3.0", indicates the following best practice for package naming convention:

    "(It's best practice to use com.*.components where *=your own designator, and use components because this is an extension of an existing component.)"

  3. Nov 06, 2008

    Here is another correction to this example

    Add the else statement after the if condition in yearsHandler function

         else
                thisYear = yearsCB.selectedItem as int;

    otherwise, thisYear will always be "currentYear", means 2008. it will be a problem when ur selected month is not Feb first, change to another year and then select Feb, you will find the day has 29, beaz thisYear is still 2008.

    BTW, replace <local:CodeBehindDisplay /> to <local:CodeBehindDisplay id="myCalendar"/>

    then you can access the value you want by myCalendar.

  4. Dec 01, 2008

    Leap years are not handled correctly by the example.

    Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.

    So the line

     has to be changed to

  5. Dec 13, 2011

    使用Code Behind可以代替<mx:script>标签了, 而且不同于<mx:script source="">里面的类只能包含函数不能写类名和包名.

    使用code behind的方法是设置xmlns:custom属性为要导入的类的包路径, 如:"as_components.*".


 

----------------------------------------

Resources