Get and save value from Dataservice Flex - apache-flex

I'm trying to learn Flex, I setup a simple Air application with PHP server as dataserice...
In my php class there is a function counttotal that return a simple int value.
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:demologicaclass="services.demologicaclass.*"
width="682" height="397" showStatusBar="false" initialize="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
protected var count:int = 0;
protected function init():void
{
counttotalResult.token = logicaservice.counttotal();
count = counttotalResult.lastResult as int;
}
protected function get_count():void
{
Alert.show(count as String);
}
.....
.....
<s:Label id="countitems" left="10" bottom="39" width="221" height="21"
fontSize="20"
fontWeight="bold" text="{counttotalResult.lastResult as String}"/>
<s:Button right="10" bottom="39" label="Controlla" click="get_count();"/>
In the label I got the correct value, but I can't save and show the value into/from a simple variable...

That's because using the keyword as will make Flex to try to cast a type to another, with a risk that the cast fails. Here, you try to cast a String as an integer, which basically means you do the following :
take 'blabla' and check if it's an integer. If it is, put its value in
count, otherwise put null in it.
What you want to do is transtype (not sure about the word, though) a String to an integer. To do this, use the following syntax :
count = int(Number(counttotalResult.lastResult));
The above means
Take lastResult, and convert it into a Number, and then into an int.
The conversion can fail (but it wont, as long as lastResult is the string representation of a valid number), and the syntax could be shorter, but in a nutshell, that's the difference between casting a type to another, and converting a type to another.

Related

Why flex string is NOT supporting unicode 0?

When I am trying to convert bytes to characters, flex stops conversion if it encounters unicoder number 0 (NUL). Why is it so? Flex is able to convert 1-256 unicode numbers except 0.
In the following example, Alert window does NOT display any text, because parameters started with 0 while forming the string message from unicode numbers.
<?xml version="1.0" encoding="utf-8"?>
<s:Application name="Alert" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="init();">
<s:controlBarContent>
<s:Button id="btn"
label="Show alert"
click="init();"/>
</s:controlBarContent>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function init():void {
// if string message value is String.fromCharCode(78,0);, then Alert displays as N
//Here, since message starts with unicode character 0, Alert displays nothing.
//Flex string is getting stopped if it encounters unicode 0, why is it so?
//but flex string supports other contorl ascii characters except NUL (0)
var message:String=String.fromCharCode(0, 78, 1);
Alert.show(message, "", Alert.YES | Alert.NO | Alert.OK | Alert.CANCEL);
}
]]>
</fx:Script>
</s:Application>
I am not sure why flex is not able to convert unicode 0 character?
Temporarily, I am converting them to 32 (empty space) if it is 0.
Thanks in advance.
Good question!. I try it on my Flash Builder 4.6.
As i Know is String.fromCharCode(num),num depend from keycode
if your get the num begin at 1~Max and it not include in keycode list,
it will return whitespace. But if the num is 0,fromCharCode will return undefined ref.
But you cloud get string length from this string array:
{undefined,h}.length = 2
{undefined,h} make to string = undefined. remember it as X.
i will show you the Math
h+X = h+undefined.
you could try this:
var str:String = String.fromCharCode(0,104);
var str1:String = String.fromCharCode(1,104);
var str2:String = String.fromCharCode(104,0,104);
Alert.show(str);
Alert.show(str.length.toString());
Alert.show(str1);
Alert.show(str1.length.toString());
Alert.show(str2);
Alert.show(str2.length.toString());
You will see str2 show you something different.
{h,undefined,h}.length = 3
for each this for string ref will be:
h+X = h+undefined.
I Guess it's reason is String.fromCharCode Function don't catch
if(codenum == 0){return whitespace}
It may use as switch or for or others way begin the code num at 1.
C strings are terminated by a null byte \0 character.
Similar to C, I believe ActionScript interprets this as a null terminated string.
Because 0 byte is not actually a string char. If you are working with binary data keep it in ByteArray

Not able to format milliseconds with DateTimeFormatter

I am not able to get milliseconds with DateTimeFormatter. I lost a few hair to this already:
<fx:Declarations>
<s:DateTimeFormatter id="dtf"
dateTimePattern="{pattern.text}"
errorText="Invalid input value"/>
</fx:Declarations>
<s:HGroup>
<s:TextInput id="pattern" />
<s:Label text="{dtf.format( new Date())}" />
</s:HGroup>
When I type "y-MM-dd HH:mm:ss.SSS#" in {pattern} , I am not seeing anything between the dot and the pound sign. Does SSS only work for parsing?
Well, that's my solutions then:
<fx:Declarations>
<s:DateTimeFormatter id="dtf"
dateTimePattern="{pattern.text}"
errorText="Invalid input value"/>
</fx:Declarations>
<s:HGroup includeIn="start" bottom="5" left="5">
<s:TextInput id="pattern" />
<s:Label text="{dtf.format( new Date()) + (new Date().milliseconds + 1000).toString().substr(1)}" />
</s:HGroup>
And the pattern would be "y-MM-dd HH:mm:ss."
Try using the mx DateFormatter formatter rather than spark DateTimeFormatter.
The format strings for date formatting in DateFormatter may are slightly different than other time/date formatters but milliseconds (Q) is supported and does work.
With Apache Flex 4.10 (and above) this code:
protected function traceDate():void
{
var df:DateFormatter = new DateFormatter()
df.formatString="YYYY-MM-DD HH:NN:SS.QQQQ#";
trace(df.format(new Date()));
}
Produces a time date string like so:
2013-11-30 09:40:11.0528#
End the date string with "." or ":"
example if the date string is "2018-04-19 16:46:49.766"
then make it "2018-04-19 16:46:49.766" + "."
or
"2018-04-19 16:46:49.766" + ":"

convert UTC time to date time format in flex?

How to convert UTC time into date time format in flex. I am using sdk 3.5. for example I have current date time in UTC format as 1309522586000 (milliseconds) and I want to convert it to friday jul 1 2011. How can I do this??
Thanks
If your are using a UNIX timestamp that you are retrieving from your server, first you will have to multiply it by 1000.
This is because UNIX timestamps are expressed in seconds whereas ActionScript timestamps are expressed in milliseconds.
You can create a date from your timestamp as follows:
var myDate:Date = new Date(1309522586000);
Next, you create a formatDate function that you call with myDate as parameter:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:DateFormatter id="myDF" formatString="EEEE MMM D YYYY"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function formatDate(date:Date):void{
trace(myDF.format(date));
}
]]>
</fx:Script>
Notice that I am using a dateformatter to format the date correctly.
More about DateFormatter and possible formats here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/formatters/DateFormatter.html
Cheers

flex3 Format date without timezone

I'm receiving a date from a server in milliseconds since 1-1-1970. I then use the DateFormatter to print the date to the screen. However, Flex adds timedifference and thus it displays a different time than what I got from the server. I've fixed this by changing the date before printing to screen. But I think that's a bad solution because the date object doesn't hold the correct date.
Does anyone know how to use the dateFormatter to print the date, ignoring the timezone?
this is how I did it:
function getDateString(value:Date):String
{
var millisecondsPerMinute:int = 1000*60;
var newDate:Date = new Date(value.time - (millisecondsPerMinute*value.timezoneOffset));
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(newDate);
}
Maybe there is something I'm missing but this seems to work for me.
<?xml version="1.0"?>
<!-- formatters\FormatterDateField.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- Declare a DateFormatter and define formatting parameters.-->
<mx:DateFormatter id="dateFormatter"
formatString="EEEE DD-MM-YYYY LL:NN:SS AA"/>
<mx:Label text="Millis (1220836618601 == Monday 08-09-2008 01:16:58 AM):"/>
<mx:TextInput id="dob" text="1220836618601"/>
<mx:Label text="Formatted date UTC: "/>
<mx:TextInput id="formattedDate"
text=""
editable="false"/>
<mx:Label text="Formatted date local: "/>
<mx:TextInput id="formattedDateLoc"
text=""
editable="false"/>
<!-- Format and update the date.-->
<mx:Button label="Format Input"
click="
var d :Date = new Date(parseInt(dob.text));
formattedDate.text=dateFormatter.format(d.toUTCString());
formattedDateLoc.text=dateFormatter.format(d);
"/>
</mx:Application>
Suggesting that instead of passing the date object (which is timezone dependant) into the dateFormatter, pass in the date object's UTC String instead. I didn't find anything that would suggest that the DateFormatter does anything to the timezone, so there shouldn't be any need to try to compensate for the timezone, especially when the date object already provides a method for getting the UTC.
function getDateString(value:Date):String
{
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";
return dateFormatter.format(value.toUTCString());
}
In Flex Hero 4.5 you can use the new Spark DateTimeFormatter:
<s:DateTimeFormatter dateTimePattern="HH':'mm':'ss" id="dateFormatterUTC" useUTC="true" />
<s:Label text="{dateFormatterUTC.format(new Date())}" />
The most simple of fixes is to have as many objects as you can (and properties of objects) be strings. The timezoneOffset solution works fine, but the timezoneOffset for many US cities changes twice during the year. The best rule -- everything is a string.

How to add mnemonic character to a regular button in Flex? (Need that for shortcuts)

How to add mnemonic character to a regular button in Flex? Show me some examples.
Here is an extended button component, that can help you deal with the task:
package test
{
import mx.controls.Button;
import flash.text.TextFormat;
public class ShortcutButton extends Button
{
private static const underlineTF:TextFormat = new TextFormat(null, null, null, null, null, true);
public function ShortcutButton()
{
super();
}
protected var shortcutPosition:int = -1;
override public function set label(value:String):void
{
shortcutPosition = value.indexOf("~");
if (shortcutPosition >= 0)
value = value.substring(0, shortcutPosition) + value.substring(shortcutPosition + 1);
super.label = value;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if (shortcutPosition > -1 && shortcutPosition < textField.text.length - 1)
textField.setTextFormat(underlineTF, shortcutPosition, shortcutPosition+1);
}
}
}
Just place "~" symbol before the symbol you want to underline to show it's a shortcut mnemonic character.
Example (you'll have to add xmlns:test="test.*" to the container component):
<test:ShortcutButton label="~Update" />
1st Solution:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
Just set the encoding value of the mxml file and you can type the
special character right in. ISO-8859-1 encoding type works for me with
the © symbol.
2nd Solution:
Let's Example, The © escape sequence is only defined for HTML, and not XML.
According to the W3C XML 1.0 recommendation, only a handful of such
mnemonic escape sequences are defined. However, you can include other
characters using numeric character references by specifying their
Unicode code points.
See: http://www.w3.org/TR/REC-xml/#syntax
For instance, the copyright symbol can be expressed as either (without
the double quotes): "©" or "&xA9;" (hexadecimal). This will work
provided the font that you're using has the glyphs for the code point
that you're referencing (as Unicode defines characters and symbols from
a very wide assortment of languages, many of which will not exist in
all fonts.
For a list of all the Unicode characters and their code points, you can
consult the Unicode code charts available here:
http://www.unicode.org/charts/
You can also lists online giving the code points for some of the more
common symbols, and most operating systems now come with a character or
symbol picker (e.g. Microsoft Windows' character map utility) that'll
oftentimes list the characters' code points as well.
http://livedocs.adobe.com/flex/3/html/help.html?content=accessible_7.html

Resources