OpenLayers OpenLayers

Ticket #653 (assigned feature)

Opened 1 year ago

Last modified 2 weeks ago

Default Parameters for HTTPRequest

Reported by: euzuro Assigned to: euzuro (accepted)
Priority: major Milestone: 2.7 Release
Component: Layer.HTTPRequest Version: 2.4 RC1
Keywords: Cc:
State:

Description (Last modified by euzuro)

Once again, we need to consider a class's constructor more carefully in the context of subclassing.

The problem that spurred this ticket is that I have a class which is a subclass of HTTPRequest, and I want it to set some default parameters.

Currently, there is no elegant way to do this without stomping over the 'params' value passed in as an argument, since it gets set in HTTPRequest constructor.

I came up with two different ways of solving my problem, and have implemented them both in this patch:

1) In HTTPRequest.initialize(), test to see if this.params is null. If it is not, then just ol.util.extend() the argument parameters to the existing value of this.params. If it *is* null, initialize it to new Object().

This solution is handy because it allows a subclass to do something like this:

    this.params = { "foo": bar };
    OpenLayers.Layer.HTTPRequest.prototype.initialize.apply(this, arguments);

And we can be guaranteed that if the 'params' entry in 'arguments' has a "foo" entry, it will override this default value of bar.

The problem here, however, is that this might lead people to think that they can simply define parameter constants in their class files, like this:

MyLayer = OpenLayers.Class.create();
MyLayer.prototype = 
  OpenLayers.Class.inherit(OpenLayers.Layer.HTTPRequest, {

    /** @type Object */
    params: {
        'version': '1.0.0'
    },
 
    ...

Whereas that seems like a lovely solution, we have the problem here of reference vs value. When the HTTPRequest constructor is called, it will append any new parameters directly to 'this.params'. Fine and dandy, except that *same* object is shared by all instances of class MyLayer. This means that every instance of MyLayer will be sharing the exact same hashtable for 'params'. Disasterous.*

How, then, can we specify some default paramters?

2) Create a new property called 'DEFAULT_PARAMS' that gets added *before* the 'params' argument with each instantiation. See the attached patch for details, but the main idea is that users can now declare a constant parameter 'DEFAULT_PARAMS' and be assured that it will be handled correctly.

* A long, long time ago, a newzealander warned me of this error. And he was a wise man.

Attachments

defaultParams.patch (1.1 kB) - added by euzuro on 04/09/07 15:56:34.

Change History

04/09/07 15:56:34 changed by euzuro

  • attachment defaultParams.patch added.

04/09/07 15:58:14 changed by euzuro

  • owner changed.
  • component changed from Layer to Layer.HTTPRequest.

04/09/07 16:02:39 changed by euzuro

  • description changed.

04/12/07 12:13:02 changed by euzuro

  • milestone changed from 2.4 Release to 2.5 Release.

I still think this is something that should be cleaned up as it will make lots of our code more readable and less susceptible to le bug.

HOwever, this is not something that really really really needs to go into 2.4

for the time being, people can just keep using the applyDefaults() method like in other parts of the code.

moving to 2.5 for a less-stressed re-evaluation

06/07/07 14:17:42 changed by sderle

  • owner set to euzuro.

06/25/07 23:52:59 changed by euzuro

  • milestone deleted.

07/04/08 02:10:26 changed by euzuro

  • status changed from new to assigned.
  • state changed.
  • milestone set to 2.7 Release.