[OpenLayers-Dev] 2.10 and 2.11-RC1 OpenLayers.Class behavior changes

Andreas Hocevar ahocevar at opengeo.org
Thu Jul 28 06:36:25 EDT 2011


Hi,

what about this:

var A = OpenLayers.Class({...});
var B = OpenLayers.Class(A, {...});

var OverloadB = {
    initialize: function() {
        alert("OverloadB");
    },
    customMethod: function() {...},
    customProperty: "foo"
};
var proto = B.prototype;
B = OverloadB.initialize;
B.prototype = proto;
OpenLayers.Util.extend(B.prototype, OverloadB);

Andreas.


On Jul 28, 2011, at 11:27 , Richard Didier wrote:

> Le mercredi 27 juillet 2011 11:39:17, Richard Didier a écrit :
>> Le mardi 26 juillet 2011 11:52:10, Andreas Hocevar a écrit :
>>> Hi Richard,
>> 
>> Hi Andreas,
>> 
>>> Also, please let me know if you're able to override constructors in 2.11
>>> with this approach:
>>> 
>>> var A = OpenLayers.Class({...});
>>> var B = OpenLayers.Class(A, {...});
>>> 
>>> OverloadB = {
>>> 
>>>    initialize: function() {...},
>>>    customMethod: function() {...},
>>>    customProperty: "foo"
>>> 
>>> };
>>> var BTemp = OverloadB.initialize;
>>> BTemp.prototpye = B.prototype;
>>> OpenLayers.Util.extend(BTemp.prototype, OverloadB);
>>> B = BTemp;
>> 
>> Based on this, I have modified and tested overloading in both OL 2.10 and
>> OL 2.11 and its works for both ! The final code looks like (sure it can be
>> improved) :
>> 
>> 
>>    /**
>>     * Function: OpenLayers.overload
>>     * Apply the patch to the given class and propagate it downward
>>     * to the sub-classes by insuring that only not overwritten
>>     * methods() or properties are overloaded.
>>     *
>>     * Parameters:
>>     * P - {Object} an instance of {<OpenLayers.Class>}
>>     * F - {Object} an object used to overwrite methods (including
>>     * constructor) and properties of P and its sub-classes.
>>     *
>>     * Returns:
>>     * {Object} the overloaded instance of given {<OpenLayers.Class>}
>>     */
>>    OpenLayers.overload= function(P,F) {
>>        ...
>>    };
>> 
>> 
>> Many thanks for your help.
>> 
> 
> I wa a bit too enthusiast on this !(
> 
> The fact is that with OL 2.11 an important part of the information seems to be lost when digging into the class 
> hierarchy with Firebug :
> 
> A= OpenLayers.Class(...);
> A.B= OpenLayers.Class(A, ...);
> 
> In the end, one has :
> 
> A
> 	B
> 	prototype
> 		initialize
> 			B
> 
> When I applied my OpenLayers.overload method, I lost the second B in the initialize function() ... Although I still 
> have B has an instanceof A, if I applied again the overload method, it does work anymore ...
> Reading the code (and to my understanding of this), it seems that the constructor does not "link" properly the B 
> class with the A class when overriding the B class ...
> 
> Any thought ?
> 
> 
> Regards,
> 
> didier
> 
>>> Andreas.
> -- 
> RICHARD Didier - Chef du pôle technique du Géoportail
> 2/4, avenue Pasteur - 94165 Saint Mandé Cedex
> Tél : +33 (0) 1 43 98 83 23

-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Dev mailing list