Ticket #823 (closed feature: fixed)

Opened 6 years ago

Last modified 6 years ago

simplify class stuff

Reported by: tschaub Owned by: tschaub
Priority: minor Milestone: 2.5 Release
Component: general Version: 2.4
Keywords: Cc:
State:

Description

All the extra typing to define classes deal with inheritance is a bother.

I like:

var MyClass = new OpenLayers.Class(Parent1, Parent2, {
    // class def here
});

Instead of:

var MyClass = new OpenLayers.Class.create();
MyClass.prototype = 
  OpenLayers.Class.inherit(Parent1, Parent2, {
    // class def here
});

Attachments

class.patch Download (10.2 KB) - added by tschaub 6 years ago.
simple class syntax
class.2.patch Download (71.3 KB) - added by tschaub 6 years ago.
new class syntax applied everywhere and tested
Format.XML.class.patch Download (503 bytes) - added by fredj 6 years ago.
fix OpenLayers.Format.XML class declaration

Change History

Changed 6 years ago by tschaub

Most of the patch is documentation, tests, and deprecated stuff. The meat of the new class syntax is in these lines:

OpenLayers.Class = function() {
    var Class = function() {
        this.initialize.apply(this, arguments);
    }
    var extended = {};
    var parent;
    for(var i=0; i<arguments.length; ++i) {
        if(typeof arguments[i] == "function") {
            // get the prototype of the superclass
            parent = arguments[i].prototype;
        } else {
            // in this case we're extending with the prototype
            parent = arguments[i];
        }
        OpenLayers.Util.extend(extended, parent);
    }
    Class.prototype = extended;
    return Class;
}

Changed 6 years ago by tschaub

simple class syntax

Changed 6 years ago by tschaub

  • keywords review added

Don't know what is up with no HTML preview on that patch.

Apply it if you dare. Approve it if you like.

Changed 6 years ago by crschmidt

This patch doesn't include the toString hacks we had in place before. I don't have IE handy, but I'm thinking this might fail tests in IE for that reason. If that is fixed (or proven to be unneccesary) then this can probably go in, but I would need to run the tests in IE (or have someone else assure me of same) before I'd mark it commit.

Changed 6 years ago by euzuro

I like this patch. A few things:

  • Foo = new OpenLayers.Class() or Foo = OpenLayers.Class() ?? seems to me that the "new" there is not necessary. Usage of it in tests, however, is inconsistent.
  • What cr5 said above is a good point. The code should be copied over. Not only that, but we should design a test for that bug, since none now exists.
  • Application! I think that the patch is good and cool and so if we're going to accept it, the patch should go through each class definition and replace it with the new style. This will be the best way of really testing the patch thoroughly.
  • this.superclass - Even though it becomes complicated in the case of multiple-inheritance, I still think it is worth it to add this feature. SDE and I started in on a sandbox to do this but never finished it. Just off the top of my head it seems like it would be something like this:
        for(var i=0; i<arguments.length; ++i) {
            if(typeof arguments[i] == "function") {
                // get the prototype of the superclass
                parent = arguments[i].prototype;
    
    
                if (i==0) {
                    Class.prototype.superclass = arguments[i];
                }
    
    
            } else {
    
    

... though not sure if that is quite right.

Changed 6 years ago by tschaub

  • keywords review removed

pulling from the review queue until I can demonstrate that this really works

Changed 6 years ago by tschaub

I've got a nice big patch ready for review. After someone reviews #837.

Changed 6 years ago by tschaub

new class syntax applied everywhere and tested

Changed 6 years ago by tschaub

  • keywords review added

Ok, please review class.2.patch. This adds the new style Class constructor, modifies all instances of Class.create and Class.inherit, allows for backwards compatibility, and tests.

Changed 6 years ago by tschaub

Patch trivia:

svn diff lib | diffstat
 ...
 98 files changed, 278 insertions(+), 416 deletions(-)
svn diff tests | diffstat
 BaseTypes/test_Class.html |  164 ++++++++++++++++++++++++++++++++++++++++++++++
 Control/test_Panel.html   |    6 -
 Marker/test_Box.html      |    2 
 3 files changed, 166 insertions(+), 6 deletions(-)

Tests pass in IE (6/7) and FF

Changed 6 years ago by euzuro

All looks good. Few comments:

  • Index: tests/Marker/test_Box.html -- the changes to this file are insignificant and I think can be removed.
  • According to my editor, the following files' changes cause the OpenLayers.Class line to go over the defined 79 character limit:
    • lib/OpenLayers/Control/EditingToolbar.js
    • lib/OpenLayers/Geometry/Polygon.js
  • Need to update Layer/TileCache.js to use the new style.
  • I would change this comment:
    /**
     * Constructor: OpenLayers.Class
     * Base class used to construct all other classes with multiple inheritance.
     * This constructor is new in OpenLayers 2.5.  At OpenLayers 3.0, the old
     * syntax for creating classes and dealing with inheritance will be removed.
     * 
     * To create a new OpenLayers style class, use the following syntax:
     * > MyClass = new OpenLayers.Class(prototype);
     *
     * To create a new OpenLayers Style class with multiple inheritance, use the
     *     following syntax:
     * > MyClass = new OpenLayers.Class(Class1, Class2, prototype);
     *
     */
    

to read:

/**
 * Constructor: OpenLayers.Class
 * Base class used to construct all other classes. Includes support for 
 *     multiple inheritance. 
 *     
 * This constructor is new in OpenLayers 2.5.  At OpenLayers 3.0, the old 
 *     syntax for creating classes and dealing with inheritance 
 *     will be removed.
 * 
 * To create a new OpenLayers-style class, use the following syntax:
 * > MyClass = new OpenLayers.Class(prototype);
 *
 * To create a new OpenLayers-style class with multiple inheritance, use the
 *     following syntax:
 * > MyClass = new OpenLayers.Class(Class1, Class2, prototype);
 *
 */

Changed 6 years ago by euzuro

  • keywords commit added; review removed

assuming you are ok with the above comments, then im ok with committing... :-)

Changed 6 years ago by tschaub

  • keywords commit removed
  • status changed from new to closed
  • resolution set to fixed

Thanks for the review Dr. Uz. All comments incorporated.

This in with r3767.

Henceforth:

var MyClass = OpenLayers.Class(SomeParent, prototype);

Changed 6 years ago by fredj

  • status changed from closed to reopened
  • resolution fixed deleted

OpenLayers.Format.XML still use the old syntax

Changed 6 years ago by fredj

fix OpenLayers.Format.XML class declaration

Changed 6 years ago by crschmidt

  • status changed from reopened to closed
  • resolution set to fixed

(In [4255]) Update class creation on Format.XML. Thanks, fredj. (Closes #823)

Note: See TracTickets for help on using tickets.