[Flex] How to Use registerclassalias to solve the problem of using remoteobject in Module

Source: Internet
Author: User

Problem: Today, I use remoteobject In the flex module to remotely call AMF data with the server. There is no problem when you switch to the module for the first time. However, when you switch to the module for the second time, an error is always reported and remote calls cannot be completed. This is confusing.

During remoting access, I used a custom parameter class:

Package Vo
{
[Remoteclass (alias = "servicelibrary. systemframework. userinfosearchcondition")]
Public class userinfosearchconditionvo
{
Public Function userinfosearchconditionvo ()
{
}
Public var Username: string;
Public var initialize mentid: string;
}
}

[Remoteclass (alias = "servicelibrary. systemframework. userinfosearchcondition")]

This is the alias definition of the local object and the remote server class, which everyone should know.

 

Remote Call:

VaR condition: userinfosearchconditionvo = new userinfosearchconditionvo ();
Condition. Username = txtsearchusername. text;
VaR resp1: responder = new Responder (onusersearchcountsuccess, onusersearchcountfaild );
NC. Call ("servicelibrary. systemframework. userinfofacade. getuserinfocountbycondition", resp1, condition );

 

But I don't know why, it is okay to load the module for the first time. This registration information will be lost when the module is loaded for the second time.

 

Solution: You need to explicitly declare the registration information,CodeAs follows:

Import flash.net. registerclassalias;
Import MX. messaging. Messages. remotingmessage;
Import VO. userinfosearchconditionvo;
Registerclassalias ("servicelibrary. systemframework. userinfosearchcondition", userinfosearchconditionvo );

The above code can be placed in your module, or directly on the application. You can declare the code once. The submodule does not need to be affirmed.

I posted the final code. If you can understand it, it would be better. I can't understand it either :)

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: module xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/halo" layout = "vertical" width = "100%" Height = "100%"
Creationcomplete = "moduleappscreationcompletehandler (event)">
<FX: SCRIPT>
<! [CDATA [
Import MX. Events. listevent;
Import appconfig. connection;


Import MX. Events. flexevent;
Import MX. Controls. Alert;
Public var NC: netconnection;
Import MX. Collections. arraycollection;
Public var pagecount: Int = 10;
Public var curpage: Int = 0;
Public var totalcount: Int = 0;

// This is where you need to declare the registration alias.
Import flash.net. registerclassalias;
Import MX. messaging. Messages. remotingmessage;
Import VO. userinfosearchconditionvo;
Registerclassalias ("servicelibrary. systemframework. userinfosearchcondition", userinfosearchconditionvo );

Protected function module1_creationcompletehandler (Event: flexevent): void
{
// Todo auto-generated method stub
NC = new netconnection ();
NC. objectencoding = objectencoding. amf3;
VaR gatewayurl: String = appconfig. Connection. getinstance (). connectionstring;

NC. addeventlistener (netstatusevent. net_status, netstatehandle );
NC. Connect (gatewayurl );
}

Protected function netstatehandle (EVT: netstatusevent): void
{
Alert. Show ("connection error! "," Error ");
}

Protected function btnsearch_clickhandler (Event: mouseevent): void
{
// Todo auto-generated method stub



Bindgrid ();


}

Protected function bindgrid (): void
{



VaR condition: userinfosearchconditionvo = new userinfosearchconditionvo ();
Condition. Username = txtsearchusername. text;

VaR resp1: responder = new Responder (onusersearchcountsuccess, onusersearchcountfaild );
NC. Call ("servicelibrary. systemframework. userinfofacade. getuserinfocountbycondition", resp1, condition );


VaR resp2: responder = new Responder (onusersearchsuccess, onusersearchfaild );
NC. Call ("servicelibrary. systemframework. userinfofacade. getuserinfobycondition", resp2, condition, curpage, pagecount );
}

Protected function onusersearchcountsuccess (Re: Object): void {
This. totalcount = re as int;
If (curpage> 0)
This. btnprev. Visible = true;
Else
This. btnprev. Visible = false;

If (curpage + 1) * pagecount> = totalcount)
This. btnnext. Visible = false;
Else
This. btnnext. Visible = true;
}
Protected function onusersearchcountfaild (Re: string): void {
Alert. Show ("An error occurred while obtaining data! "," Error ");
}

Protected function onusersearchsuccess (Re: arraycollection): void {
Griduser. dataprovider = Re;
}
Protected function onusersearchfaild (Re: string): void {
Alert. Show ("An error occurred while obtaining data! "," Error ");
}

Protected function btnnext_clickhandler (Event: mouseevent): void
{
// Todo auto-generated method stub
Curpage ++;
Bindgrid ();
}


Protected function btnprev_clickhandler (Event: mouseevent): void
{
// Todo auto-generated method stub
Curpage --;
Bindgrid ();

}

Protected function griduser_itemclickhandler (Event: listevent): void
{
// Todo auto-generated method stub
This. currentstate = "edit ";
}

]>
</FX: SCRIPT>
<Mx: States>
<S: State name = "View"/>
<S: State name = "edit"/>
</MX: States>
<S: hgroup width = "100%" Height = "100%">

<S: panel width = "250" Height = "100%" Title = "user management">
<S: layout>
<S: verticallayout/>
</S: layout>
<S: hgroup width = "100%" Height = "29">
<S: textinput id = "txtsearchusername" Height = "29" width = "162"/>
<Mx: button id = "btnsearch" label = "query mailto:" % 20 icon = "@ embed (Source = 'Assets/images/search.png ') "Height =" 29 "labelplacement =" right "Click =" btnsearch_clickhandler (event) ">

</MX: button>
</S: hgroup>
<Mx: hrule width = "100%"/>
<Mx: buttonbar Height = "20" height. view = "32">
<Mx: dataprovider>
<FX: Object Label = "add mailto:" % 20 icon = "@ embed (Source = 'Assets/images/add.png ')"/>
<FX: Object Label = "delete mailto:" % 20 icon = "@ embed (Source = 'Assets/images/remove.png ')"/>
</MX: dataprovider>
</MX: buttonbar>
<Mx: DataGrid id = "griduser" width = "100%" Height = "100%" itemclick = "griduser_itemclickhandler (event)">
<Mx: columns>
<Mx: datagridcolumn headertext = "User Name" datafield = "username"/>

</MX: columns>
</MX: DataGrid>
<S: hgroup x = "393" Y = "264" width = "100%" Height = "26">
<Mx: linkbutton label = "Previous Page" id = "btnprev" visible = "false" Click = "btnprev_clickhandler (event)"/>
<Mx: linkbutton label = "next page" id = "btnnext" visible = "false" Click = "btnnext_clickhandler (event)"/>
</S: hgroup>
</S: Panel>
<Mx: vrule Height = "100%" basecolor = "# a75757"/>
<S: Panel id = "pnluseredit" horizontalcenter = "0" verticalcenter = "0" width = "100%" Height = "100%" dropshadowvisible = "true" Title = "user information"
Includein = "edit">
<S: layout>
<S: verticallayout/>
</S: layout>

<Mx: Form width = "100%" Height = "100%">
<Mx: formitem label = "account:" required = "true">
<S: textinput id = "Account">

</S: textinput>
</MX: formitem>
<Mx: formitem label = "Password:" required = "true">
<S: textinput id = "PWD" displayaspassword = "true">

</S: textinput>

</MX: formitem>
<Mx: formitem>
<S: checkbox id = "chkrememberme" selected = "true" label = "remember me">

</S: checkbox>
</MX: formitem>
<Mx: formitem horizontalalign = "right">
<S: button label = "login" id = "btnsave">

</S: button>
</MX: formitem>

</MX: Form>

</S: Panel>
</S: hgroup>
</MX: module>

 

 

Finally, I would like to emphasize my design principles:

When remotely calling data between the client and the server, I do not advocate the "simple parameters" mode. For example, to query the User Function, I can use username, deparmentid... but there is a scalability problem: when you want to add a "query condition" in the future, you have to modify the interface. This will cause the client to modify n calling locations, and the server also needs to modify the interface, which will cause a great expansion problem.

Therefore, we recommend that you customize the correspondence between a "condition Query Class" and a "server" so that only one interface parameter is required and the type remains unchanged.

If you want to extend the "query condition" in the future, you only need to add an attribute on the "client" and assign a value to it. On the "server" page, you only need to add attributes and modify the implementation code. You do not need to modify the "interfaces" at both ends ..

 

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.