[OpenLayers-Dev] "design pattern" question

Tim Schaub tschaub at openplans.org
Mon Apr 7 11:56:21 EDT 2008


Hey-

Bart van den Eijnden (OSGIS) wrote:
> Hi list,
> 
> I've been wresting with the following use case for quite some time now, so I
> am seeking advice here.
> 
> My use case is the following:
> 
> 1) I have a layer which is of type OpenLayers.Layer.WMS
> 2) to get the WFS associated with this layer, I perform an SLD WMS
> DescribeLayer request, this gives me back the location of the WFS and the
> name of the typename
> 3) using the info from step 2), I perform a WFS DescribeFeatureType request
> and parse the result in order to get a list of attributes for this layer.
> 
> I've got this code working using a sequence of OpenLayers.LoadURL calls, but
> now I want to create a utility function which wraps the above functionality
> into 1 function. Can this be done without reverting to a synchronous call? I
> need the response from 2) in order to do 3).
> 
> I.e. this is what I have now:
> 
> var url =  layer.getFullRequestString({REQUEST: "DescribeLayer"});
> OpenLayers.loadURL(url, '', this, this.parseDescribeLayer);
> 
> ..
> 
> parseDescribeLayer: function(response) {
>   // parse the result and call a DescribeFeatureType on the WFS
>   ..
>   OpenLayers.loadURL(url, '', this, this.parseAttributes);
> }
> 
> ..
> 
> parseAttributes: function(response) {
>   var descfeaturetype = new  OpenLayers.Format.WFSDescribeFeatureType();
>   var attributes = descfeaturetype.read(response.responseText);
>   this.layer.attributes = attributes;
> }
> 
> What I would want to do is:
> 
> OpenLayers.Util.getAttributes(layer)
> 
> which performs all of the above and only returns when it has finished all
> operations.

Event driven design requires different thinking.  There are tricks that 
approximate what you want, but you're better off having your 
getAttributes function trigger a callback when the asynchronous sequence 
completes.

That said, there is nothing stopping you from defining 
parseDescribeLayer and parseAttributes within getAttributes.

function getAttributes(layer, callback) {
     function parseAttributes: function(response) {
         // parse response.responseText
         callback.call(attributes);
     }
     function parseDescribeLayer(response) {
         // parse
         OpenLayers.loadURL(url, null, null, parseAttributes);
     }
     OpenLayers.loadURL(url, null, null, parseDescribeLayer);
}

I suspect this is obvious, and you find it nicer to define those as 
public methods on some other object (more efficient that way).

Anyway, I'd say if your utility method involves asynchronous calls, it 
should take a callback as an argument.

The fact is, we've got a single thread.  If you just can't put up with 
this, you could consider writing in something like Narrative JavaScript 
[1] and compiling your code to JavaScript.

Tim

[1] http://neilmix.com/narrativejs/doc/index.html


> 
> Best regards,
> Bart
> 
> --
> Bart van den Eijnden
> OSGIS, Open Source GIS
> http://www.osgis.nl
> 
> 
> 
> 
> 
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev
> 
> !DSPAM:4033,47fa07b7180331961014482!
> 




More information about the Dev mailing list