Interaction between flex and python backend (original)

Source: Internet
Author: User
Flex has a variety of ways to interact with the background, (/Files/xingluzhe/flex.ppt of the Web Service) Here is a good introduction. I used python's django as the backend. I have made some achievements and shared them with you.
The first is the establishment of the Django background. This is introduced in one of my blog posts and I will not go into details here.
The second is the interaction between the front-end of flex and the python backend.
1. Create a. xml configuration file (in the same directory as the mxml file ). The configuration file contains the following content:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Services-config>
<Services>
<Service id = "webblogService" class = "flex. messaging. services. RemotingService"
MessageTypes = "flex. messaging. messages. RemotingMessage">
<Destination id = "webblogapp">
<Channels>
<Channel ref = "webblogChannel"/>
</Channels>
<Properties>
<Source> * </source>
</Properties>
</Destination>
</Service>
</Services>
<Channels>
<Channel-definition id = "webblogChannel" class = "mx. messaging. channels. AMFChannel">
<Endpoint uri = "http: // localhost: 8080/webblog/gateway/" class = "flex. messaging. endpoints. AMFEndpoint"/>
</Channel-definition>
</Channels>
</Services-config>
2. Add the following statement to the main mxml file:
<Mx: RemoteObject
Id = "djangoService"
Destination = "webblogapp" // note that the destination here is the folder generated by using python manage. py startapp webblogapp. It does not work if you have not tried the name line.
ShowBusyCursor = "true">
</Mx: RemoteObject>
Note that only one RemoteObject is used in my entire project. In other component applications, the RemoteObject defined on the home page mainly uses Application. application. djangoService.
3. Interaction with the background
Private var token: AsyncToken; // defines a token.
Token = remoteObj. echo (remotefunc, arg); // call the interface echo provided by the background database
Token. addResponder (new AsyncResponder (onResultHandle, faultHandler); // Add responder for token
Then, process the results returned by the background in onResultHandle.
Private function onResultHandle (re: ResultEvent, token: Object = null): void
{
ResultHandle (re); // perform corresponding processing here
}

Private function faultHandler (fe: FaultEvent, token: Object = null): void
{
Trace (fe. fault. faultDetail); // this function can be used as an original copy, because when an error occurs in your background, it prints the error location and cause in the flex console window.
}
4. From the above introduction, we can see that each call to a method in the background will repeat the same operation, except that the called method is different. To achieve code reuse, I wrote a simple class to communicate with the background:/Files/xingluzhe/KCommunication.txt (Code of this class)
//////////////////////////////////////// /////////////////
// FileName: KCommunication.
// Creator: David (Cai Haibin)
// Date: 2009-8-20
// Commment: Mainly used for frontend and backend communication.
//////////////////////////////////////// /////////////////
Package com. wps
{
Import flash. events. EventDispatcher;

Import mx. core. Application;
Import mx. rpc. AsyncResponder;
Import mx. rpc. AsyncToken;
Import mx. rpc. events. FaultEvent;
Import mx. rpc. events. ResultEvent;
Import mx. rpc. remoting. RemoteObject;

Public class KCommunication extends EventDispatcher
{
/**
* @ Brief class Constructor
*
*/
Public function KCommunication ()
{
}

/**
* Set Token and corresponding processing function
* @ Param remotefunc the remote function to be called
* @ Param arg the parameter to be passed to the remote function (all are put in a string)
* @ Param func: RESPONSE event after interaction with the background
*
*/
Public function setTokenAndHandle (remotefunc: String, arg: String, func: Function): void
{
ResultHandle = func;
Token = remoteObj. echo (remotefunc, arg); // Note: here, the background interface is unified as echo, and the passed parameters are all strings.
Token. addResponder (new AsyncResponder (onResultHandle, faultHandler ));
}

/**
* @ Brief: RESPONSE event after interaction with the background
* @ Param re
* @ Param token
*
*/
Private function onResultHandle (re: ResultEvent, token: Object = null): void
{
ResultHandle (re); // note that this processing function can be provided outside the class.
}

Private function faultHandler (fe: FaultEvent, token: Object = null): void
{
Trace (fe. fault. faultDetail );
}

Private var remoteObj: RemoteObject = Application. application. djangoService ;;
Private var resultHandle: Function = null;
Private var token: AsyncToken;
}
}
For example:
For example, to implement the login function
1. import com. KCommunication;

2. private var comm: KCommunication = new KCommunication ();

3. private function onConfirm (evt: Event): void
{
// GetUser is an interface defined in the background to return the user name and password. Here validateUserInfo is used to process the returned result.
Comm. setTokenAndHandle ("getUser", username. text, validateUserInfo );
}
4. private function validateUserInfo (re: ResultEvent): void
{
If (re. result. length> 0)
{
_ Name = re. result [0]. username. toString ();
_ Pw = re. result [0]. password. toString ();
_ Nick = re. result [0]. nickname. toString ();
If (_ name = username. text & _ pw = password. text)
{
Kgv. _ username = _ name;
Kgv. _ password = _ pw;
Kgv. _ nickname = _ nick;

Username. text = "";
Password. text = "";
}
Else
{
ErrorMessage ();
}
}
Else
{
ErrorMessage ();
}
}
In this way, communication can be realized.
Now I have the last question: the implementation of the unified interface in the background ^_^
Def getUser (name ):
# Return the user
User = Users. objects. filter (username = name [0])
Return user

// This is a unified interface defined by me in the background
Def echo (request, requestFunc, requestArg): // The first parameter is required and does not need to be specified during the call. requestFunc is the method provided to the foreground, and requestArg is the string parameter,
With |-> !!!! <-| Separator
# Seperate the arguments out at first
Arg = requestArg. split ('|-> !!!! <-| ') // Separate the required parameters (all parameters here are strings. You can install the required parameters to convert the string to the required parameter type.
Return FuncMap [requestFunc] (arg) // call the function. FuncMap is a Map container.

FuncMap is defined as follows:
FuncMap = {"getUser": getUser, # when add an interface, register here
}
When you add a new interface, you only need to register it here.

---- David Cai later than Kingsoft

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.