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

RICHARD Didier didier.richard at ign.fr
Tue Aug 2 05:48:35 EDT 2011


>>> Really not sure : I am still stuck with constructor modifications (when
>>> sub-classes use the same constructor as the superclass whose initialize
>>> method is beeing modified) in the class hierarchy in OL 2.11 rc1. I had
>>> no
>>> problem with OL 2.10 ...
>>
>> Could you please provide a small test case that fails? (The simplest
>> possible please)
>
> My apologies if you've already provided one. I'm coming back from vac,
> and catching up with (lots of) things...
>

Here is my last shot :



    A= OpenLayers.Class({
        initialize:function() { this.p_= "A.initialize";
OpenLayers.Console.info(this.p_); },
        pa:"PA",
        p_:null,
        ma:function() { OpenLayers.Console.info("A.ma="+this.pa); }
    });
    A["B"]= OpenLayers.Class(A,{
        pa:"PA-bis",
        pb:"PB",
        mb:function() { OpenLayers.Console.info("B.mb="+this.pa+"
"+this.pb); }
    });
    A["B"]["D"]= OpenLayers.Class(A.B,{
        initialize:function() { this.p_= "D.initialize";
OpenLayers.Console.info(this.p_); }
    });
    A["B"]["E"]= OpenLayers.Class(A.B,{
        initialize:function() { this.p_= "E.initialize";
OpenLayers.Console.info(this.p_); }
    });
    A.C= OpenLayers.Class(A,{
        initialize:function() { this.p_= "C.initialize";
OpenLayers.Console.info(this.p_); },
        pc:"PC",
        ma:function() { OpenLayers.Console.info("C.ma="+this.pa+"
"+this.pc); }
    });
    A.C.F= OpenLayers.Class(A.C,{
        pf:"PF",
        mf:function() { OpenLayers.Console.info("F.mf="+this.pa+"
"+this.pc+" "+this.pf); }
    });
    A.C.G= OpenLayers.Class(A.C,{
        pg:"PG",
        ma:function() { OpenLayers.Console.info("G.ma="+this.pa+"
"+this.pc+" "+this.pg); }
    });


    /**
     * 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) {
        if (typeof(F.initialize)=="function") {
            var X= OpenLayers.Util.extend({},P);
            var xProtoInitialize= P.prototype.initialize;
            var initialize= F.initialize;
            delete F.initialize;
            var pTemp= P.prototype;
            P= initialize;
            P.prototype= pTemp;
            OpenLayers.Util.extend(P,X);
            X= null;
            // override sub-class having same constructor:
            for (var pn in P) {
                if (typeof(P[pn])=='function' &&
P[pn].prototype.initialize===xProtoInitialize) {
                    /*OpenLayers.Console.info(pn+" same constructor ...");*/
                    var ppn= P[pn];
                    delete P[pn];
                    var f= null;
                    eval('f= {"initialize":'+initialize.toString()+'}');
                    P[pn]= OpenLayers.overload(ppn,f);
                }
            }
        }
        OpenLayers.Util.extend(P.prototype, F);

        return P;
    };

    OpenLayers.Console.log("==============================================
overloading A:");
    A= OpenLayers.overload(A, {
        initialize:function() { this.p_= "A.initialize-new";
OpenLayers.Console.info(this.p_); },
        pa:"PA-new"
    });
    A.C= OpenLayers.overload(A.C, {
        initialize:function() { this.p_= "C.initialize-new";
OpenLayers.Console.info(this.p_); },
        ac:{'foo':'bar'}
    });

    OpenLayers.Console.log("A:");
    try {
        var a2= new A();
        OpenLayers.Console.assert((a2 instanceof A));
        OpenLayers.Console.assert((a2.p_=="A.initialize-new"));
        a2.ma();
    } catch(exa) {
        OpenLayers.Console.warn(exa);
    }

    OpenLayers.Console.log("A.B:");
    try {
        var b2= new A.B();
        OpenLayers.Console.assert((b2 instanceof A));
        OpenLayers.Console.assert((b2 instanceof A.B));
        OpenLayers.Console.assert((b2.p_=="A.initialize-new"));
        b2.ma();
        b2.mb();
    } catch(exb) {
        OpenLayers.Console.warn(exb);
    }

    OpenLayers.Console.log("A.B.D:");
    try {
        var d2= new A.B.D();
        OpenLayers.Console.assert((d2 instanceof A));
        OpenLayers.Console.assert((d2 instanceof A.B));
        OpenLayers.Console.assert((d2 instanceof A.B.D));
        OpenLayers.Console.assert((d2.p_=="D.initialize"));
        d2.ma();
        d2.mb();
    } catch(exd) {
        OpenLayers.Console.warn(exd);
    }

    OpenLayers.Console.log("A.B.E:");
    try {
        var e2= new A.B.E();
        OpenLayers.Console.assert((e2 instanceof A));
        OpenLayers.Console.assert((e2 instanceof A.B));
        OpenLayers.Console.assert((e2 instanceof A.B.E));
        OpenLayers.Console.assert((e2.p_=="E.initialize"));
        e2.ma();
        e2.mb();
    } catch(exe) {
        OpenLayers.Console.warn(exe);
    }

    OpenLayers.Console.log("A.C:");
    try {
        var c2= new A.C();
        OpenLayers.Console.assert((c2 instanceof A));
        OpenLayers.Console.assert((c2 instanceof A.C));
        OpenLayers.Console.assert((c2.p_=="C.initialize-new"));
        OpenLayers.Console.assert((typeof(c2.ac)=='object' &&
c2.ac.foo=='bar'));
        c2.ma();
    } catch(exc) {
        OpenLayers.Console.warn(exc);
    }

    OpenLayers.Console.log("A.C.F:");
    try {
        var f2= new A.C.F();
        OpenLayers.Console.assert((f2 instanceof A));
        OpenLayers.Console.assert((f2 instanceof A.C));
        OpenLayers.Console.assert((f2 instanceof A.C.F));
        OpenLayers.Console.assert((f2.p_=="C.initialize-new"));
        OpenLayers.Console.assert((typeof(g2.ac)=='object' &&
g2.ac.foo=='bar'));
        f2.ma();
        f2.mf();
    } catch(exf) {
        OpenLayers.Console.warn(exf);
    }

    OpenLayers.Console.log("A.C.G:");
    try {
        var g2= new A.C.G();
        OpenLayers.Console.assert((g2 instanceof A));
        OpenLayers.Console.assert((g2 instanceof A.C));
        OpenLayers.Console.assert((g2 instanceof A.C.G));
        OpenLayers.Console.assert((g2.p_=="C.initialize-new"));
        OpenLayers.Console.assert((typeof(g2.ac)=='object' &&
g2.ac.foo=='bar'));
        g2.ma();
    } catch(exg) {
        OpenLayers.Console.warn(exg);
    }

    OpenLayers.Console.log("done!");



This test has been ran on FF successfully.

>
>
> --
> Eric Lemoine
>
> Camptocamp France SAS
> Savoie Technolac, BP 352
> 73377 Le Bourget du Lac, Cedex
>
> Tel : 00 33 4 79 44 44 96
> Mail : eric.lemoine at camptocamp.com
> http://www.camptocamp.com
>


-- 
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


More information about the Dev mailing list