Apr 19

girl-128x128No matter what kind language using programmer will always accumulate many useful and daily using function with rich in development experience day by day. And then the accumulation become to a library at last. Of course it’s include the flex programmers. Following is some very basic, very simple functions which I’ve collected. How about you? If have any proudly and recommendable functions? Then, don’t only stock in your head, leave your comments. Let’s share. 


No.1 Copy content to clipboard:

  1. System.setClipboard(strContent);

No.2 Clone an ArrayCollection:

  1. //dummy solution( well, it works )
  2. var bar:ArrayCollection = new ArrayCollection();
  3. for each ( var i:Object in ac ){ 
  4. bar.addItem( i );
  5. } 
  6. // fantastic ! //
  7. var bar:ListCollectionView = new ListCollectionView( ListCollectionView( ac ).list );

No.3 Open URL:

  1. navigateToURL(new URLRequest('http://ntt.cc'), '_blank'

No.4 Page reload:

  1. navigateToURL(new URLRequest("javascript:location.reload();"),"_self")

No.5 Close browse:

  1. navigateToURL(new URLRequest("javascript:window.close()"),"_self");

No.6 Set the background alpha to transparent on Alert window:

  1. Alert 
  2. { 
  3. modalTransparency:0.0;  
  4. modalTransparencyBlur:0;
  5. }

No.7 Set random color:

  1. lbl.setStyle('color', 0xffffff*Math.random());

No.8 Trim left of white space

  1. public function LTrim(s : String):String 
  2. { 
  3.   var i : Number = 0;
  4.   while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 || s.charCodeAt(i) == 9) 
  5.   { 
  6.     i++;
  7.   } 
  8.   return s.substring(i,s.length);
  9. }

No.9 Trim right of white space

  1. public function RTrim(s : String):String 
  2. { 
  3.   var i : Number = s.length - 1;
  4.   while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 ||s.charCodeAt(i) == 9) 
  5.   { 
  6.     i--;
  7.   } 
  8.   return s.substring(0,i+1);
  9. }

No.10 Trim left and right of white space

  1. public function Trim(s : String):String 
  2. { 
  3.   return LTrim(RTrim(s));
  4. }

No.11 get data type:

  1. getQualifiedClassName(data)

No.12 Generate check digits

  1. private function GenerateCheckCode():String 
  2. { 
  3.     //init
  4.     var ran:Number;
  5.     var number:Number;
  6.     var  code:String;
  7.     var checkCode:String ="";
  8.     //get 4 radom
  9.    for(var i:int=0; i<4; i++) 
  10.    { 
  11.        ran=Math.random();
  12.        number =Math.round(ran*10000);            //get result like 0.1234
  13.        if(number % 2 == 0) 
  14.          code = String.fromCharCode(48+(number % 10));        //0's ASCII code is 48
  15.        else 
  16.          code = String.fromCharCode(65+(number % 26)) ;        // A's ASCII code is 65
  17.        checkCode += code;
  18.    } 
  19.    return checkCode;
  20. }

written by Ntt.cc   |  tags: , , ,

Related Post

15 Responses to “12 very simple,basic but useful function source in Flex”

  1. Dusty Jewett Says:

    Two things about the ‘cloning’ of the ArrayCollection. First off, creating a ListCollectionView is generally a poor idea, as some functions specifically require an ArrayCollection. Secondly, this creates a new object, but it wraps the same array… You’ll get strange results when you add and remove objects to either object. This code will create a new object that is completely independent of the old.

    var newArray:ArrayCollection = new ArrayCollection(oldAC.source.slice());

    Also, for trimming strings, StringUtil.trim() works well… it trims both sides, but the code used there is very efficient, you can copy the start/end portions for your own use.

  2. Darren Says:

    Another option to clone an ArrayCollection is:

    var newAC:ArrayCollection = new ArrayCollection(oldAC.toArray());

    This will create a new ArrayCollection from an array that is populated in the same order as the old ArrayCollection.

    Also, should number 11 be:

    No.11 get *data* type:

  3. Dallas Clark Says:

    I’m surprised that the functions LTrim and RTrim are not already in the Flex framework.

  4. julien Says:

    Great idea of posting that list … kudos … after reading your post I was wondering if you or any other members of the AS/Flash/Flex community would be interested in a “smart” AS cheat sheet where we’d put all this usefull info.
    Cheers

  5. eli Says:

    ltrim(), rtrim(), and trim() already exist in com.adobe.utils StringUtil as mentioned by the first commenter.

    What reason is there not to use those functions? I can’t thing of one…

  6. 5566 Says:

    I feel regular expression suits trimming function better.

  7. minidxer Says:

    @Dusty Jewett ,Darren, Dallas Clark, eli

    Thanks your comments.
    Strickly to say I’m not a member of the AS/Flash/Flex community,
    I’m just a C++ programmer and be used to write some basic function by myself. If you have interesting could see here for details:
    http://ntt.cc/2008/02/12/flex-vs-ajax%E2%80%94both-have-their-pros-and-cons.html
    I’ve mentioned before.

    @ julien
    I just want to share useful information for everyone sincerely. and there is nothing for others. But I think you are very “smart”, :)

  8. Chuck Says:

    var newArray:ArrayCollection = new ArrayCollection(oldAC.source.slice());
    var newAC:ArrayCollection = new ArrayCollection(oldAC.toArray());

    The problem with both of these is they produce a shallow copy of the array. In the developer guide page 156 and 157 it talks about making a deep copy of arrays.
    It goes like this:

    import flash.utils.ByteArray;
    function clone(source:Object):*
    {
    var myBA:ByteArray = new ByteArray();
    myBA.writeObject(source);
    myBA.position = 0;
    return(myBA.readObject());
    }

    This will work for both indexed and associated arrays.

  9. efish Says:

    Simple but useful
    ——————————
    toString(16)转化成16进制字符串。最大只能表达0×7FFFFFFF。表示RGB够用,但是表示ARGB (alpha, red, green, blue )不够。to hex string
    toString(2)转换成2进制字符串。to binary string
    ——————————-
    tip:parseInt可以转换字符串到int,第二个参数是基数。
    // decimal:
    trace(parseInt(”16711884″)); // 16711884
    // hexadecimal:
    trace(parseInt(”FF00CC”,16)); // 16711884
    // binary:
    trace(parseInt(”111111110000000011001100″,2)); // 16711884

  10. Joeflash Says:

    Regarding #4 and #5, the use of “javascript:” in a URLRequest has been prohibited under the new 9.0.124 player security restrictions.

Leave a Reply