Split & Join: What you need to know about replace operation on Strings!

Friday, August 31, 2012

Once upon a time long long ago, in ActionScript 2, String.replace method was missing (something that can be seen in most other major programming languages).

After many years, 'replace' method came along in String class of ActionScript 3. But, not many folks were happy. Because, in ActionScript 3, string.replace(arg1, arg2), replaces only the first occurrence of the arg1 with arg2.

In order to perform a ‘replace all’ operation use

strTarget.replace(/arg1/g, arg2);
The 'g' is just a handle to specify that all occurrences of the string arg1 need to be replaced

or
 
strTarget.split(arg1).join(arg2);

Point to note here is that, in the both the methods, traditional 'replace' & the 'split-join' method arg1 can be of type string or a regular expression as the need be.

Chronicles of Sockets in Flash: the cross-domain, the Sockets & the Ports!

Connecting to a server-side socket from Flash using ActionScript 3.0


Cross domain policy

Its absolutely, undoubtedly not possible to connect to a UDP socket from within Flash (unless of course you are within the application sandbox of an AIR application).

Outside of AIR, Flash can only connect to TCP/IP sockets that too the ones that are specified in the cross-domain xml. This is true even if the swf domain is same as that of the target socket.

Also, the cross domain xml needs to be served by a socket on the same domain (not necessarily the same port as the target socket).

For Flash Player 10 onwards, this xml should come as plain text response over the socket (basically to avoid any HTTP headers).

Moral of the story: UDP not possible (as of now) if you are working on a web-based Flash (non-AIR) application. And, cross-domain policy file needs to be server over a socket & not as an xml file from a web server (even if) on the target domain.

A typical cross-domain xml looks like this.
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy>   <allow-access-from domain="*" secure="false" to-ports="843,1232-1237"> < /cross-domain-policy>

Ensure that you have mentioned the ports that need to access the socket properly in the "to-ports" attribute.
In the above example, the list of all allowed ports are: 843, 1232,1233, 1234, 1235, 1236, and 1237.

Also, if the cross-domain is not working even if it seems that you have done everything right, then try adding a EOL character at the end of the cross-domain string.

Hope this helps!

Flex 4: Truncating labels in a Tree Component

Wednesday, August 29, 2012


Recently, I stumbled upon quite a bunch of questions on forums regarding truncating the labels of entries in the Tree Component.

The solution, scattered in many places, not that obvious, is quite easy.

All you need to do is use the 'truncateToFit' property with your Tree Item Renderer.

The different ways of achieving this when using an MXML item renderer & an ActionScript class file as renderer are mentioned below:


1. When using an MXML based item renderer

In this case, all one needs to do is add truncateToFit="true" to the Label tag in the MXML file.
<mx:label color="#00ff00" font="font" id="labelField" paddingtop="2" text="{treeListData.label}" truncateToFit="true" width="100" >


2. When using an ActionScript class based item renderer

In this case, just add a single line at the end of the updateDisplayList overridden method as shown below:


override protected function updateDisplayList (unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
....
this.label.truncateToFit();

}

When working with a class based renderer, 'truncateToFit' acts as a method that returns a Boolean value to inform whether truncate action was required or not. Also, it can accept a single parameter that you wish to use instead of the ellipsis (by default ellipsis '...' are used). This might be useful to somebody :)

Flex 4: Using 'truncateToFit' to truncate longer labels? Don't forget to mention 'maxWidth'!

Recently, I stumbled across another of the few tricks that are 'good-to-know' when it comes to flex development.

In order to make life easier Flex offers a property called truncateToFit with its lable component, thereby offsetting the developer to manually handle truncating long labels & appending '...' at the end.

But, it is noteworthy that only setting the truncateToFit Boolean does not suffice. You need to also mention the 'maxWidth' and/or 'width' property for the label.

<mx:Label text="Hello World!" truncateToFit="true" maxWidth="180"/>
 

About Me

My photo
Front-end Developer with over 9 years of experience in developing Web 2.0 sites with a keen eye for UX. Avid technology enthusiast; gadget freak; old school PC gamer; loves developing Apps & Data Visualizations.

My Website

Stack Overflow profile

Twitter Profile

Facebook profile