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

Eric Lemoine eric.lemoine at camptocamp.com
Tue Aug 2 09:54:30 EDT 2011


On Tue, Aug 2, 2011 at 3:23 PM, RICHARD Didier <didier.richard at ign.fr> wrote:
>
>> On Tue, Aug 2, 2011 at 12:54 PM, RICHARD Didier <didier.richard at ign.fr>
>> wrote:
>>>
>>>>> This test has been ran on FF successfully.
>>>>
>>>> I haven't looked at your tests into detail, but do mean your problems
>>>> are now solved?
>>>>
>>>
>>> no, just meant it works with OL 2.11 under FF ... I have got problems
>>> with
>>> OL 2.10 !
>>
>> I fail to see where you're having problem. Sorry.
>>
>
> What about these cases ?
>
> 1/ var A= OpenLayers.Class({...})
> A.staticFunction= function() { ... };
> A= overload(A, { initialize: function() {...}});

See my changes to the overload function, and test_overload_6.

>
> 2/ in test 4, if you remove the initialize function from B and overload A,
> the B constructor is still the previous A's constructor.

Yes. To address this one I see no other solution than patching
OpenLayers.Class. See the patch attached to this email, and my
test_overload_5 test function. The Class.html tests continue to pass
with my patch.



<html>
<head>
  <!--<script src="http://openlayers.org/api/2.10/OpenLayers.js"></script>-->
  <!--<script src="http://openlayers.org/dev/OpenLayers.js"></script>-->
  <script src="OLLoader.js"></script>
  <script type="text/javascript">

    // the overload function under test
    function overload(C, o) {
        if(typeof o.initialize === "function" &&
            C === C.prototype.initialize) {
            // OL 2.11

            var proto = C.prototype;
            var staticProps = OpenLayers.Util.extend({}, C);

            C = o.initialize;

            C.prototype = proto;
            OpenLayers.Util.extend(C, staticProps);
        }
        OpenLayers.Util.extend(C.prototype, o);
        return C;
    }

    function test_overload_1(t) {
        // overload constructor
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        A = overload(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var a = new A;
        t.eq(a.a, "bar", "ctor overloaded");
    }

    function test_overload_2(t) {
        // overload regular method
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
            },
            method: function() {
                this.a = "foo";
            }
        });
        A = overload(A, {
            method: function() {
                this.a = "bar";
            }
        });
        var a = new A;
        a.method();
        t.eq(a.a, "bar", "method overloaded");
    }

    function test_overload_3(t) {
        // overload constructor of subclass
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {
            initialize: function() {
                A.prototype.initialize.call(this);
            }
        });
        B = overload(B, {
            initialize: function() {
                A.prototype.initialize.call(this);
                this.a = "bar";
            }
        });
        var b = new B;
        t.eq(b.a, "bar", "ctor overloaded");
    }

    function test_overload_4(t) {
        // overload constructor of parent class
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {
            initialize: function() {
                A.prototype.initialize.call(this);
            }
        });
        A = overload(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var b = new B;
        t.eq(b.a, "bar", "ctor overloaded");
    }

    function test_overload_5(t) {
        // overload constructor of parent class
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {});
        A = overload(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var b = new B;
        t.eq(b.a, "bar", "ctor overloaded");
    }

    function test_overload_6(t) {
        // with static methods
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
            }
        });
        A.staticMethod = function() {};
        A = overload(A, {
            initialize: function() {
            }
        });
        var exc = false;
        try {
            A.staticMethod();
        } catch(e) {
            exc = true;
        }
        t.ok(!exc, "static method still there");
    }

  </script>
</head>
<body>
</body>
</html>


-- 
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20110802/3503e3d3/overload.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Class.patch
Type: text/x-diff
Size: 498 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20110802/3503e3d3/Class.bin


More information about the Dev mailing list