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

Richard Didier didier.richard at ign.fr
Wed Jul 27 05:39:17 EDT 2011


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) {
        //save old class
        var xProto= OpenLayers.Util.extend({},P.prototype);
        var X= {};
        for (var xn in P) {
            var p= P[xn];
            if (typeof(p)=="function" && p.prototype.initialize) {
                // clean P from sub-classes ...
                delete P[xn];
                X[xn]= p;
            }
        }
        //save overload object to prevent overriding of initialize method in it
        var f= OpenLayers.Util.extend({},F);

        var pTemp= null;
        // protect initializer from sub-classes overloading :
        if (typeof(F.initialize)=="function") {
            pTemp= function() { F.initialize.apply(this,arguments); };
            delete f["initialize"];
        } else {
            var initialize= P.prototype.initialize;
            pTemp= function() { initialize.apply(this,arguments); };
            delete P.prototype["initialize"];
        }
        pTemp.prototype= P.prototype;
        OpenLayers.Util.extend(pTemp.prototype, f);
        P= pTemp;

        // restore sub-classes:
        // propagate constructor/properties/method to sub-class ...
        for (var xn in X) {
            var p= X[xn];
            delete X[xn];
            if (typeof(p)=='function') {
                if (p.prototype.initialize) {//ensure it is a Class
                    f= OpenLayers.Util.extend({},F);
                    // check for overloaded properties :
                    for (var fn in F) {
                        var fp= F[fn];
                        if (typeof(fp)=="function") {
                            if (p.prototype[fn]===xProto[fn]) {
                                // override method ...
                                continue;
                            }
                            delete f[fn];
                            continue;
                        }
                        if (p.prototype.hasOwnProperty(fn) && xProto.hasOwnProperty(fn) && fp!==undefined) {
                            if (p.prototype[fn]===xProto[fn]) {
                                // override property ...
                                continue;
                            }
                            delete f[fn];
                            continue;
                        }
                    }
                    // override sub-class :
                    var x= p;
                    var c= OpenLayers.Class(P,p.prototype);
                    for (var cn in x) {
                        var xp= x[cn];
                        delete x[cn];
                        if (typeof(xp)=='function' && xp.prototype.initialize) {//ensure it is a Class
                            c[cn]= xp;
                        }
                    }
                    x= null;
                    P[xn]= OpenLayers.overload(c,f);
                }
            } else {
                P[xn]= p;
            }
        }
        xProto= null;
        X= null;

        return P;
    };


Many thanks for your help.

> Andreas.

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