Adobe Solution Partner

April 9, 2009

Shan’s Simple Examples: Extending an object in Actionscript

Filed under: Flex & Flash, Shan's Simple Examples — Tags: , — Shannon Hicks @ 4:20 pm

The other day, someone asked me how they could re-use the code sample from Igor Costa’s post about getting the Rich Text Editor to output XHTML. Whenever you want to add functionality to an existing control, you just need to extend it. Here’s how:

Here’s the top bit of code from my XHtmlEditor.as file:

1
2
3
4
5
6
7
8
9
10
package org.iotashan.controls
{
    import mx.controls.RichTextEditor;
   
    public class XHtmlEditor extends mx.controls.RichTextEditor
    {
        public function XHtmlEditor()
        {
            super()
        }

The actual code where I’m telling the compiler that this object extends RichTextEditor is line 5. You can then see that in the constructor method, I call super() on line 9. The super() method just calls the original constructor of the RichTextEditor object.

From here, we define a getter and setter method for a property called xhtml. The xhtml property doesn’t actually store any value in our object, we just use it as an interface to the convertToXHtml() and convertFromXHtml() methods from Igor’s blog post.

12
13
14
15
16
17
18
        public function get xhtml():String {
            return convertToXHtml(this.htmlText);
        }
       
        public function set xhtml(val:String):void {
            this.htmlText = convertFromXHtml(val);
        }

You can view the full working example here, and get the source here.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

 

Server Down?

Maximize Web application uptime by drawing upon Webapper's years of experience tuning and stabilizing many of the world's largest ColdFusion Web applications. Contact us today!