@@ -21,12 +21,13 @@ function popuphelp(url)
2121 ) ;
2222}
2323
24- Function . prototype . pwgBind = function ( ) {
25- var __method = this , object = arguments [ 0 ] , args = Array . prototype . slice . call ( arguments , 1 ) ;
26- return function ( ) {
27- return __method . apply ( object , args . concat ( arguments ) ) ;
28- }
24+ function pwgBind ( object , method ) {
25+ var args = Array . prototype . slice . call ( arguments , 2 ) ;
26+ return function ( ) {
27+ return method . apply ( object , args . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
28+ }
2929}
30+
3031function PwgWS ( urlRoot )
3132{
3233 this . urlRoot = urlRoot ;
@@ -39,106 +40,116 @@ function PwgWS(urlRoot)
3940} ;
4041
4142PwgWS . prototype = {
42-
4343 callService : function ( method , parameters , options )
4444 {
4545 if ( options )
4646 {
47- for ( var property in options )
48- this . options [ property ] = options [ property ] ;
47+ for ( var prop in options )
48+ this . options [ prop ] = options [ prop ] ;
4949 }
50- try { this . transport = new XMLHttpRequest ( ) ; }
50+ try { this . xhr = new XMLHttpRequest ( ) ; }
5151 catch ( e ) {
52- try { this . transport = new ActiveXObject ( 'Msxml2.XMLHTTP' ) ; }
52+ try { this . xhr = new ActiveXObject ( 'Msxml2.XMLHTTP' ) ; }
5353 catch ( e ) {
54- try { this . transport = new ActiveXObject ( 'Microsoft.XMLHTTP' ) ; }
54+ try { this . xhr = new ActiveXObject ( 'Microsoft.XMLHTTP' ) ; }
5555 catch ( e ) {
56- dispatchError ( 0 , "Cannot create request object" ) ;
56+ this . error ( 0 , "Cannot create request object" ) ;
57+ return ;
5758 }
5859 }
5960 }
60- this . transport . onreadystatechange = this . onStateChange . pwgBind ( this ) ;
61+ this . xhr . onreadystatechange = pwgBind ( this , this . onStateChange ) ;
6162
6263 var url = this . urlRoot + "ws.php?format=json" ;
6364
6465 var body = "method=" + method ;
6566 if ( parameters )
6667 {
67- for ( var property in parameters )
68+ for ( var prop in parameters )
6869 {
69- if ( typeof parameters [ property ] == 'object' && parameters [ property ] )
70+ if ( typeof parameters [ prop ] == 'object' && parameters [ prop ] )
7071 {
71- for ( var i = 0 ; i < parameters [ property ] . length ; i ++ )
72- body += "&" + property + "[]=" + encodeURIComponent ( parameters [ property ] [ i ] ) ;
72+ for ( var i = 0 ; i < parameters [ prop ] . length ; i ++ )
73+ body += "&" + prop + "[]=" + encodeURIComponent ( parameters [ prop ] [ i ] ) ;
7374 }
7475 else
75- body += "&" + property + "=" + encodeURIComponent ( parameters [ property ] ) ;
76+ body += "&" + prop + "=" + encodeURIComponent ( parameters [ prop ] ) ;
7677 }
7778 }
7879
7980 if ( this . options . method == "POST" )
80- {
81- this . transport . open ( this . options . method , url , this . options . async ) ;
82- this . transport . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
83- this . transport . send ( body ) ;
84- }
81+ this . xhr . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
8582 else
8683 {
8784 url += "&" + body ;
88- this . transport . open ( this . options . method , url , this . options . async ) ;
89- this . transport . send ( null ) ;
85+ body = null ;
86+ }
87+ this . xhr . open ( this . options . method , url , this . options . async ) ;
88+ try {
89+ this . xhr . send ( body ) ;
90+ } catch ( e ) {
91+ this . error ( 0 , e . message ) ;
9092 }
9193 } ,
9294
9395 onStateChange : function ( ) {
94- var readyState = this . transport . readyState ;
96+ var readyState = this . xhr . readyState ;
9597 if ( readyState == 4 )
96- this . respondToReadyState ( readyState ) ;
98+ {
99+ try {
100+ this . respondToReadyState ( readyState ) ;
101+ } finally {
102+ this . cleanup ( ) ;
103+ }
104+ }
97105 } ,
98106
99- dispatchError : function ( httpCode , text )
107+ error : function ( httpCode , text )
100108 {
101109 ! this . options . onFailure || this . options . onFailure ( httpCode , text ) ;
110+ this . cleanup ( ) ;
102111 } ,
103112
104113 respondToReadyState : function ( readyState )
105114 {
106- var transport = this . transport ;
107- if ( readyState == 4 && transport . status == 200 )
115+ var xhr = this . xhr ;
116+ if ( readyState == 4 && xhr . status == 200 )
108117 {
109118 var resp ;
110119 try {
111- eval ( ' resp = ' + transport . responseText ) ;
120+ resp = window . JSON && window . JSON . parse ? window . JSON . parse ( xhr . responseText ) : ( new Function ( "return " + xhr . responseText ) ) ( ) ;
112121 }
113122 catch ( e ) {
114- this . dispatchError ( 200 , e . message + '\n' + transport . responseText . substr ( 0 , 512 ) ) ;
123+ this . error ( 200 , e . message + '\n' + xhr . responseText . substr ( 0 , 512 ) ) ;
115124 }
116125 if ( resp != null )
117126 {
118127 if ( resp . stat == null )
119- this . dispatchError ( 200 , "Invalid response" ) ;
128+ this . error ( 200 , "Invalid response" ) ;
120129 else if ( resp . stat == 'ok' )
121- {
122- if ( this . options . onSuccess ) this . options . onSuccess ( resp . result ) ;
123- }
130+ ! this . options . onSuccess || this . options . onSuccess ( resp . result ) ;
124131 else
125- this . dispatchError ( 200 , resp . err + " " + resp . message ) ;
132+ this . error ( 200 , resp . err + " " + resp . message ) ;
126133 }
127134 }
128- if ( readyState == 4 && transport . status != 200 )
129- this . dispatchError ( transport . status , transport . statusText ) ;
135+ if ( readyState == 4 && xhr . status != 200 )
136+ this . error ( xhr . status , xhr . statusText ) ;
130137 } ,
131138
139+ cleanup : function ( )
140+ {
141+ if ( this . xhr ) this . xhr . onreadystatechange = null ;
142+ this . xhr = null ;
143+ this . options . onFailure = this . options . onSuccess = null ;
144+ } ,
132145
133- transport : null ,
134- urlRoot : null ,
135- options : { }
146+ xhr : null
136147}
137148
138149function pwgAddEventListener ( elem , evt , fn )
139150{
140- if ( window . attachEvent )
141- elem . attachEvent ( 'on' + evt , fn ) ;
142- else
151+ if ( window . addEventListener )
143152 elem . addEventListener ( evt , fn , false ) ;
153+ else
154+ elem . attachEvent ( 'on' + evt , fn ) ;
144155}
0 commit comments