OpenLayers OpenLayers

Changeset 7335

Show
Ignore:
Timestamp:
06/09/08 19:51:38 (2 years ago)
Author:
tschaub
Message:

Adding cross-browser XMLHttpRequest functionality and convenience methods around it in the OpenLayers.Request namespace. Deprecating OpenLayers.Ajax.Request. Full support sync/async requests using all HTTP verbs now. r=elemoine (closes #1565)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/build/license.txt

    r5614 r7335  
    4444**/ 
    4545 
     46/** 
     47 * Contains XMLHttpRequest.js <http://code.google.com/p/xmlhttprequest/> 
     48 * Copyright 2007 Sergey Ilinsky (http://www.ilinsky.com) 
     49 * 
     50 * Licensed under the Apache License, Version 2.0 (the "License"); 
     51 * you may not use this file except in compliance with the License. 
     52 * You may obtain a copy of the License at 
     53 * http://www.apache.org/licenses/LICENSE-2.0 
     54 */ 
  • trunk/openlayers/lib/OpenLayers.js

    r6818 r7335  
    8585            "Rico/Color.js", 
    8686            "OpenLayers/Ajax.js", 
     87            "OpenLayers/Request.js", 
     88            "OpenLayers/Request/XMLHttpRequest.js", 
    8789            "OpenLayers/Events.js", 
    8890            "OpenLayers/Projection.js", 
  • trunk/openlayers/lib/OpenLayers/Ajax.js

    r7039 r7335  
    3131 
    3232 
    33 /**  
    34 * @param {} request 
    35 */ 
     33/** 
     34 * Function: OpenLayers.nullHandler 
     35 * @param {} request 
     36 */ 
    3637OpenLayers.nullHandler = function(request) { 
    3738    alert(OpenLayers.i18n("unhandledRequest", {'statusText':request.statusText})); 
     
    4041/**  
    4142 * Function: loadURL 
    42  * Background load a document. 
     43 * Background load a document.  For more flexibility in using XMLHttpRequest, 
     44 *     see the <OpenLayers.Request> methods. 
    4345 * 
    4446 * Parameters: 
     
    5052 * onComplete - {Function} Optional callback for success.  The callback 
    5153 *     will be called with this set to caller and will receive the request 
    52  *     object as an argument. 
     54 *     object as an argument.  Note that if you do not specify an onComplete 
     55 *     function, <OpenLayers.nullHandler> will be called (which pops up an 
     56 *     alert dialog). 
    5357 * onFailure - {Function} Optional callback for failure.  In the event of 
    5458 *     a failure, the callback will be called with this set to caller and will 
    55  *     receive the request object as an argument. 
     59 *     receive the request object as an argument.  Note that if you do not 
     60 *     specify an onComplete function, <OpenLayers.nullHandler> will be called 
     61 *     (which pops up an alert dialog). 
    5662 * 
    5763 * Returns: 
    58  * {XMLHttpRequest}  The request object.  To abort loading, call 
    59  *     request.abort(). 
     64 * {<OpenLayers.Request.XMLHttpRequest>}  The request object. To abort loading, 
     65 *     call request.abort(). 
    6066 */ 
    6167OpenLayers.loadURL = function(uri, params, caller, 
    6268                                  onComplete, onFailure) { 
    63  
    64     var success = (onComplete) ? OpenLayers.Function.bind(onComplete, caller) 
    65                                 : OpenLayers.nullHandler; 
    66  
    67     var failure = (onFailure) ? OpenLayers.Function.bind(onFailure, caller) 
    68                            : OpenLayers.nullHandler; 
    69  
    70     // from prototype.js 
    71     var request = new OpenLayers.Ajax.Request( 
    72         uri,  
    73         { 
    74             method: 'get',  
    75             parameters: params, 
    76             onComplete: success,  
    77             onFailure: failure 
    78         } 
    79     ); 
    80     return request.transport; 
     69     
     70    if(typeof params == 'string') { 
     71        params = OpenLayers.Util.getParameters(params); 
     72    } 
     73    var success = (onComplete) ? onComplete : OpenLayers.nullHandler; 
     74    var failure = (onFailure) ? onFailure : OpenLayers.nullHandler; 
     75     
     76    return OpenLayers.Request.GET({ 
     77        url: uri, params: params, 
     78        success: success, failure: failure, scope: caller 
     79    }); 
    8180}; 
    8281 
     
    264263/** 
    265264 * Class: OpenLayers.Ajax.Request 
     265 * *Deprecated*.  Use <OpenLayers.Request> method instead. 
    266266 * 
    267267 * Inherit: 
  • trunk/openlayers/lib/OpenLayers/Format/KML.js

    r7311 r7335  
    250250     */ 
    251251    fetchLink: function(href) { 
    252         var request = new OpenLayers.Ajax.Request(href,  
    253                       {method: 'get', asynchronous: false }); 
    254  
    255         if (request && request.transport) { 
    256             return request.transport.responseText; 
     252        var request = OpenLayers.Request.GET({url: href, async: false}); 
     253        if (request) { 
     254            return request.responseText; 
    257255        } 
    258256    }, 
  • trunk/openlayers/lib/OpenLayers/Layer/GML.js

    r6573 r7335  
    101101    loadGML: function() { 
    102102        if (!this.loaded) { 
    103             var results = OpenLayers.loadURL(this.url, null, this, this.requestSuccess, this.requestFailure); 
     103            OpenLayers.Request.GET({ 
     104                url: this.url, 
     105                success: this.requestSuccess, 
     106                failure: this.requestFailure, 
     107                scope: this 
     108            }); 
    104109            this.loaded = true; 
    105110        }     
  • trunk/openlayers/lib/OpenLayers/Layer/GeoRSS.js

    r7015 r7335  
    101101        if (!this.loaded) { 
    102102            this.events.triggerEvent("loadstart"); 
    103             OpenLayers.loadURL(this.location, null, this, this.parseData); 
     103            OpenLayers.Request.GET({ 
     104                url: this.location, 
     105                success: this.parseData, 
     106                scope: this 
     107            }); 
    104108            this.loaded = true; 
    105109        }     
     
    128132     * 
    129133     * Parameters: 
    130      * ajaxRequest - {XMLHttpRequest}  
     134     * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}  
    131135     */ 
    132136    parseData: function(ajaxRequest) { 
  • trunk/openlayers/lib/OpenLayers/Layer/MapGuide.js

    r6748 r7335  
    203203            getVisParams.format = 'text/xml'; 
    204204            getVisParams = OpenLayers.Util.extend(getVisParams, params); 
    205                  
    206             new OpenLayers.Ajax.Request(this.url,  
    207                   { parameters: getVisParams, 
    208                     method: 'get', 
    209                     asynchronous: false   //must be synchronous call to return control here 
    210                   }); 
     205             
     206            OpenLayers.Request.GET({ 
     207                url: this.url, params: getVisParams, async: false 
     208            }); 
    211209          } 
    212210           
  • trunk/openlayers/lib/OpenLayers/Layer/Text.js

    r7334 r7335  
    110110 
    111111                this.events.triggerEvent("loadstart"); 
    112                 OpenLayers.loadURL(this.location, null,  
    113                                    this, this.parseData, onFail); 
     112                OpenLayers.Request.GET({ 
     113                    url: this.location, 
     114                    success: this.parseData, 
     115                    failure: onFail, 
     116                    scope: this 
     117                }); 
    114118                this.loaded = true; 
    115119            } 
     
    138142     * 
    139143     * Parameters: 
    140      * ajaxRequest - {XMLHttpRequest}  
     144     * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}  
    141145     */ 
    142146    parseData: function(ajaxRequest) { 
  • trunk/openlayers/lib/OpenLayers/Layer/WFS.js

    r7311 r7335  
    456456 
    457457        var data = this.writer.write(this.features); 
    458          
    459         var url = this.url; 
    460  
    461         var success = OpenLayers.Function.bind(this.commitSuccess, this); 
    462  
    463         var failure = OpenLayers.Function.bind(this.commitFailure, this); 
    464          
    465         // from prototype.js 
    466         new OpenLayers.Ajax.Request(url,  
    467                          {   method: 'post',  
    468                              postBody: data, 
    469                              onComplete: success,  
    470                              onFailure: failure 
    471                           } 
    472                          ); 
     458 
     459        OpenLayers.Request.POST({ 
     460            url: this.url, 
     461            data: data, 
     462            success: this.commitSuccess, 
     463            failure: this.commitFailure, 
     464            scope: this 
     465        }); 
    473466    }, 
    474467 
  • trunk/openlayers/lib/OpenLayers/Tile/WFS.js

    r6573 r7335  
    3333    /**  
    3434     * Property: request  
    35      * {OpenLayers.Ajax.Request}  
     35     * {<OpenLayers.Request.XMLHttpRequest>}  
    3636     */  
    3737    request: null,      
     
    101101    /**  
    102102    * Method: loadFeaturesForRegion 
    103     * get the full request string from the ds and the tile params  
    104     *     and call the AJAX loadURL().  
     103    * Abort any pending requests and issue another request for data.  
    105104    * 
    106105    * Input are function pointers for what to do on success and failure. 
     
    114113            this.request.abort(); 
    115114        } 
    116         this.request = OpenLayers.loadURL(this.url, null, this, success); 
     115        this.request = OpenLayers.Request.GET({ 
     116            url: this.url, 
     117            success: success, 
     118            failure: failure, 
     119            scope: this 
     120        }); 
    117121    }, 
    118122     
     
    123127    * 
    124128    * Parameters: 
    125     * request - {XMLHttpRequest
     129    * request - {<OpenLayers.Request.XMLHttpRequest>
    126130    */ 
    127131    requestSuccess:function(request) { 
  • trunk/openlayers/tests/Ajax.html

    r6719 r7335  
    55 
    66    function test_Ajax_loadUrl(t) { 
    7         t.plan(1); 
    8         var req = OpenLayers.Ajax.Request; 
    9         OpenLayers.ProxyHost = "/?url="; 
    10         OpenLayers.Ajax.Request.prototype.request = function(uri) { 
    11             t.eq(uri, "/?url=http%3A%2F%2Fexample.com%2F%3Fformat%3Dimage%2Bkml", "URI matches what we expect from loadurl"); 
    12         } 
    13         OpenLayers.loadURL("http://example.com/?format=image+kml"); 
     7        t.plan(5); 
     8        var _get = OpenLayers.Request.GET; 
     9        var caller = {}; 
     10        var onComplete = function() {}; 
     11        var onFailure = function() {}; 
     12        var params = {}; 
     13        OpenLayers.Request.GET = function(config) { 
     14            t.eq(config.url, "http://example.com/?format=image+kml", "correct url") 
     15            t.eq(config.params, params, "correct params"); 
     16            t.eq(config.scope, caller, "correct scope"); 
     17            t.ok(config.success === onComplete, "correct success callback"); 
     18            t.ok(config.failure === onFailure, "correct failure callback"); 
     19        }; 
     20        OpenLayers.loadURL("http://example.com/?format=image+kml", params, caller, onComplete, onFailure); 
     21        OpenLayers.Request.GET = _get; 
    1422    } 
    1523  </script> 
  • trunk/openlayers/tests/Tile/WFS.html

    r6724 r7335  
    6565        var g_Success = {};         
    6666 
    67         var tLoadURL = OpenLayers.loadURL
    68         OpenLayers.loadURL = function(url, params, caller, onComplete) { 
    69             t.ok(url == tile.url, "tile's url correctly passed as 1st param to loadURL"); 
    70             t.ok(params == null, "null passed as 2nd param to loadURL"); 
    71             t.ok(caller == tile, "tile passed as 3rd param to loadURL"); 
    72             t.ok(onComplete == g_Success, "success param from loadFeaturesForRegion() passed as 4th param to loadURL"); 
     67        var _get = OpenLayers.Request.GET
     68        OpenLayers.Request.GET = function(config) { 
     69            t.ok(config.url == tile.url, "tile's url correctly passed"); 
     70            t.ok(config.params == null, "null params"); 
     71            t.ok(config.scope == tile, "tile passed as scope"); 
     72            t.ok(config.success == g_Success, "success passed"); 
    7373        }; 
    7474         
     
    8383        }; 
    8484        OpenLayers.Tile.WFS.prototype.loadFeaturesForRegion.apply(tile, [g_Success]); 
    85                  
    86         OpenLayers.loadURL = tLoadURL
     85 
     86        OpenLayers.Request.GET = _get
    8787    } 
    8888     
  • trunk/openlayers/tests/list-tests.html

    r6832 r7335  
    5858    <li>Layer.html</li> 
    5959    <li>Renderer.html</li> 
     60    <li>Request.html</li> 
     61    <li>Request/XMLHttpRequest.html</li> 
    6062    <li>Layer/EventPane.html</li> 
    6163    <li>Layer/FixedZoomLevels.html</li> 
  • trunk/openlayers/tests/manual/ajax.html

    r6408 r7335  
    11<html xmlns="http://www.w3.org/1999/xhtml"> 
    22  <head> 
    3     <title>Ajax Acceptance Test</title> 
    4     <style type="text/css"> 
    5      
    6         body { 
    7             font-size: 0.8em; 
    8         } 
    9         p { 
    10             padding-top: 1em; 
    11         } 
    12          
    13         .buttons { 
    14             margin: 1em; 
    15             float: left; 
    16         } 
    17  
    18     </style> 
    19  
     3    <title>XHR Acceptance Test</title> 
    204    <script src="../../lib/OpenLayers.js"></script> 
    215    <script type="text/javascript"> 
    226        var url = "ajax.txt"; 
    237        function sendSynchronous(){ 
    24             var request = new OpenLayers.Ajax.Request(url, { 
    25                'asynchronous': false, 
    26                 onComplete: function() { 
     8            var request = OpenLayers.Request.GET({ 
     9                url: url, 
     10                async: false, 
     11                callback: function() { 
    2712                    document.getElementById('send_sync').value += 'request completed\n'; 
    2813                } 
     
    3116        } 
    3217        function sendAsynchronous(){ 
    33             var request = new OpenLayers.Ajax.Request(url, { 
    34                 onComplete: function() { 
     18            var request = OpenLayers.Request.GET({ 
     19                url: url, 
     20                callback: function() { 
    3521                    document.getElementById('send_sync').value += 'request completed\n'; 
    3622                } 
     
    3925        } 
    4026        function sendAndAbort(){ 
    41             var request = new OpenLayers.Ajax.Request(url, { 
    42                 onComplete: function(request) { 
    43                     if (request.responseText == '') { 
    44                         document.getElementById('send_sync').value += 'request aborted\n'; 
    45                     } 
     27            var request = OpenLayers.Request.GET({ 
     28                url: url, 
     29                callback: function() { 
     30                    document.getElementById('send_sync').value += 'never called\n'; 
    4631                } 
    4732            }); 
    48             request.transport.abort(); 
     33            request.abort(); 
    4934            document.getElementById('send_sync').value += 'other processing\n'; 
    5035        } 
    5136 
    52     </script> 
    53   </head> 
    54   <body > 
    55     <div class="buttons"> 
    56         <button onclick="sendSynchronous()">Send an synchronous Ajax request</button><br /> 
    57         <button onclick="sendAsynchronous()">Send an asynchronous Ajax request</button><br /> 
    58         <button onclick="sendAndAbort()">Send a request and abort it</button><br /> 
     37        </script> 
     38    </head> 
     39    <body > 
     40        <button onclick="sendSynchronous()">synchronous</button> 
     41        expected output: "request completed" then "other processing"<br /> 
     42        <button onclick="sendAsynchronous()">asynchronous</button> 
     43        expected output: "other processing" then "request completed"<br /> 
     44        <button onclick="sendAndAbort()">send and abort</button> 
     45        expected output: "other processing" (and not "never called")<br /> 
    5946        <textarea id="send_sync" rows="6"></textarea><br /> 
    6047        <button onclick="document.getElementById('send_sync').value = ''">Clear</button> 
    61     </div> 
    62     <p><b></b></p> 
    63     <p>Clicking on the different buttons should give the following results in the textarea below :</p> 
    64     <ul> 
    65       <li>synchronous: "request completed" then "other processing"</li> 
    66       <li>asynchronous: "other processing" then "request completed"</li> 
    67       <li>abort: "request aborted" then "other processing" (note that real XHR behavior would not call onComplete with abort - meaning "request aborted" would not be displayed here)</li> 
    68     </ul> 
    6948  </body> 
    7049</html>