PDA

View Full Version : few flex questions


knucklehead
03-15-2009, 04:58 AM
Hello.
I have lots of dilemmas regarding Adobe Flex since I am totally new in that programming language so I would like you to help me with this. I will compare everything with php/mysql because it's the easiest way to describe what my dilemma is about...

So, my first question is:
when I use php, I like to use lots of 'include' statements in my 'application'. For example, my index.php page usually looks like this:

<?
include("includes/session.php");
include("includes/html_header.php");

if($session->logged_in){
include ('in/menu.php');
include ('in/details.php');
include ('in/other_menu.php');

if (isset($_GET['c'])) {
$page = $_GET['c'];
include ('in/'.$page.'.php');
} else {
include ('in/info.php');
}
} else {
include ('login_form.php');
}
?>

It doesn't matter what this script does, important thing is that I found some 'flexibility' here and I easily manage everything in my application. I know that Flex can work the same way but I also know that there are few ways to accomplish what I need. For example, I could do it with <mx:State> (and I think it is the most similar to my include() stuff from php), next, I could do it with <mx:ViewStack> but I'm not sure if I can 'include' my file in each stack? Then, I can do it with <mx:ToggleButtonBar> etc etc etc.

What is the best option for me here considering that I need to pass variables all around my 'includes'?


Second question:
If i choose to use <mx:State> method for my needs, and in each <mx:State> I include a custom component, how do i send requests from one custom component to another, or to my main application?
For example, i have a page.mxml with some form and a button:
<mx:TextInput
id="loginUser"/>
<mx:TextInput displayAsPassword="true"
id="loginPass"/>
<mx:Button label="submit" id="submitButton"click="login.send();" />
but my HTTPService and login functions are in main application file and it looks like this:
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;

public function checkLogin(event:ResultEvent):void{

if (event.result.loginsuccess == "yes"){
currentState = "logged in";
}

if (event.result.loginsuccess == "no"){
currentState = "not logged in";
}
}

]]>
</mx:Script>

<mx:HTTPService id="login" result="checkLogin(event)" method="POST" url="login.php"/>
<mx:request xmlns="">
<username>{loginUser.text}</username>
<password>{loginPass.text}</password>
</mx:request>
</mx:HTTPService>
Here I get an error on page.mxml which says
1120: Access of undefined property login
But if I put exactly same form and button inside my <mx:State> in main application (not including it as a custom component) everything works fine. I am generally confused about that =)


Third question:
Is there a 'standard' iterating through results in Flex? (this probably sounds stupid but I will try to explain)
In php I would use this code to iterate through results:
<?
while($row = mysql_fetsch_array($query)){
echo $row['blah']
}
?>
I know that there is an thing in Flex which is used for that (dataProvider) but what if I want to get some results from XML and display them as few <mx:Label> which cannot use dataProvider option?


Fourth (and final) quesion:
What is the best way to handle all my queries if I use mysql as my database? For now I made few php files which get data from mysql and generates XML output which I use in Flex. I hope that there is other way than creating php files for each custom query I need. I also wonder how do I insert, update and delete records from mysql with Flex?


I hope you will find some time to explain me even a single thing from my list of mega-noobish questions =)
Thank you for your patience and effort to make me understand these basics =)

Peter Cowling
03-15-2009, 12:17 PM
Hi,

Q1
<mx:state> is really for when you quickly want to split a view into two or more parts. As you note it has the benefits you get with an include(), in that all code is 'inline'.

The problem with states, is that your code quickly becomes a mess, because it lacks modularity. By splitting things up, by using parts of the mx framework, or by using AS3, you get much greater scope for adding additional volume or detail to your application.

Q2
Flex is an event-driven framework, and communication between components therefore requires you to dispatch, and then handle an event. For more information on how to do this, see: dispatch (http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001640.html); handle (http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001640.html).

More specifically, you mention the error generated by 'login'. The problem is that the httpservice is not inline with the component making the call. The component making the call does not know anything about the other component, which is ultimately a good thing. The event based approach is a part of the solution (there are other things the help too).

Q3
If you need to create a lot of labels, you can use or extend some of the flex framework. Things like lists, repeaters, along with itemrenderers can be hooked-up to a dataprovider, and can take-in a label and other display components.

Q4
There are quite a few of options.

You can use something like AMFPHP/Zend - Wade Arnold's blog is here (http://wadearnold.com/blog/), which if you are familiar with PHP would be a good way to go. There are other solutions - sabeAMF perhaps - in this direction.

BlazeDS can work with mySQL, and is Adobe's open source solution, but it is Java.

You can work without using the AMF protocol, passing about xml, but it is not my preferred approach.

You can look into livecycle or coldfusion.