]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/RequestManager.js
2 * A management class for all request sent to the server
3 * Basically, to access the server, always get a new Request object from this class.
4 * It will then trigger various user-interface events during the Request lifecycle.
6 * For the moment, it's about the "Stop" button command, handling any passed ILoadStatusable states,
7 * and logging the Request status/errors.
9 * @author Charles du Jeu
11 qx
. Class
. define ( "org.argeo.ria.remote.RequestManager" ,
14 extend
: qx
. core
. Object
,
18 * Triggered on the user demand at the end of the Request
20 "reload" : "org.argeo.ria.event.ReloadEvent"
23 construct : function (){
29 * Sets the unique "stop" command of the application.
30 * @param stopCommand {org.argeo.ria.event.Command} The command
32 setStopCommand : function ( stopCommand
){
33 this . command
= stopCommand
;
37 * Creates a Request and handle various parts of its lifecycle.
38 * @see org.argeo.ria.event.ReloadEvent
39 * @see org.argeo.ria.components.ILoadStatusable
41 * @param url {String} The server url
42 * @param method {String} Connexion method (POST, GET, etc.)
43 * @param responseType {String} Expected response mime type (application/xml, etc...).
44 * @param fireReloadEventType {String} On user-demand, if this parameter is not null, a org.argeo.ria.event.ReloadEvent will be triggered when the request is completed.
45 * @param iLoadStatusables {Array} An array of ILoadStatusable implementations that need to be updated by the Request state (loading/ended).
46 * @return {qx.io.remote.Request}
48 getRequest : function ( url
, method
, responseType
, fireReloadEventType
, iLoadStatusables
){
49 var request
= new qx
. io
. remote
. Request ( url
, method
, responseType
);
51 request
. setUserData ( "iLoadStatusables" , iLoadStatusables
);
53 if ( fireReloadEventType
){
54 request
. addListener ( "completed" , function ( response
){
55 this . fireReloadEvent ( fireReloadEventType
, response
. getContent ());
58 this . enableCommand ( request
);
59 request
. addListener ( "timeout" , this . requestTerminated
, this );
60 request
. addListener ( "failed" , this . requestTerminated
, this );
61 request
. addListener ( "aborted" , this . requestTerminated
, this );
62 request
. addListener ( "completed" , this . requestCompleted
, this );
67 * Creates a ReloadEvent and fire it.
68 * @param dataType {String} The data type
69 * @param content {mixed} The content of the request response.
71 fireReloadEvent : function ( dataType
, content
){
72 this . fireEvent ( "reload" , org
. argeo
. ria
. event
. ReloadEvent
, [ dataType
, content
]);
76 * Triggered when request is created
77 * @param e {qx.event.type.Event} The event
79 requestCreated : function ( e
){
80 var request
= e
. getTarget ();
81 this . enableCommand ( request
);
85 * Triggered when request is completed normally
86 * @param e {qx.event.type.Event} The event
88 requestCompleted : function ( e
){
89 var request
= e
. getTarget ();
90 this . disableCommand ( request
);
94 * Triggered when request is completed abnormally
95 * @param e {qx.event.type.Event} The event
97 requestTerminated : function ( e
){
98 var request
= e
. getTarget ();
99 var errorType
= e
. getType ();
100 this . disableCommand ( request
);
102 if ( errorType
== "aborted" ){
103 message
= "Request aborted by user" ;
104 } else if ( errorType
== "failed" ){
105 message
= "Request failed!" ;
106 } else if ( errorType
== "timeout" ){
107 message
= "Request timed out!" ;
113 * Triggered by a request creation. Update the GUI parts according to its status.
114 * @param request {qx.io.remote.Request} The current Request
116 disableCommand : function ( request
){
117 this . command
. setEnabled ( false );
118 if ( request
. getUserData ( "iLoadStatusables" )){
119 this . updateGuiParts ( request
. getUserData ( "iLoadStatusables" ), false );
121 var listener
= request
. getUserData ( "listener" );
123 this . command
. removeListener ( "execute" , listener
);
128 * Triggered by a request ending. Update the GUI parts according to its status.
129 * @param request {qx.io.remote.Request} The current Request
131 enableCommand : function ( request
){
132 this . command
. setEnabled ( true );
133 if ( request
. getUserData ( "iLoadStatusables" )){
134 this . updateGuiParts ( request
. getUserData ( "iLoadStatusables" ), true );
136 qx
. ui
. core
. queue
. Manager
. flush ();
137 var listener
= request
. abort
;
138 request
. setUserData ( "listener" , listener
);
139 this . command
. addListener ( "execute" , listener
, request
);
143 * Update the ILoadStatusable implementations
144 * @param iLoadStatusables {Array} An array of ILoadStatusable
145 * @param loadStatus {Boolean} The current status of a request
147 updateGuiParts : function ( iLoadStatusables
, loadStatus
){
148 for ( var i
= 0 ; i
< iLoadStatusables
. length
; i
++){
149 if ( qx
. Class
. implementsInterface ( qx
. Class
. getByName ( iLoadStatusables
[ i
]. classname
), org
. argeo
. ria
. components
. ILoadStatusable
)){
150 iLoadStatusables
[ i
]. setOnLoad ( loadStatus
);
152 this . debug ( "Does not implement the ILoadStatusable interface! GUIPART type : " + iLoadStatusables
[ i
]. classname
);