[OpenLayers-Dev] newbie q: how to create a new class that is not inherited

Andreas Hocevar ahocevar at opengeo.org
Mon Sep 6 04:09:09 EDT 2010


Hi,

On Sep 6, 2010, at 06:50 , crb wrote:

> 
> I'm new to OL (and fairly new to JavaScript) and want to create new JS
> classes for my app so that all objects and methods are within the app
> namespace.  The classes are not subclasses of OL objects but I'd like to use
> the OL framework since it seems to make sense (maybe not?).  I've gotten
> fairly close but I'm not able to define a dot-delimited namespace w/o
> getting 'is not defined' errors.  
> 
> Here's an example that works using a classname of just 'BaseMap':
> 
> 
> HTML:
> <html>
> <head>
> <script type="text/javascript" src="myclass.js"></script>
>    <script type="text/javascript">
> 
>      function initBasemap() {
>          var myBaseMap = new BaseMap();
>      }
> 
>    </script>
> </head>
> <body onload="initBasemap()">
> <div id="map" class="midmap"></div>
> </body>
> </html>
> 
> JAVASCRIPT (myclass.js):
> 
> BaseMap = OpenLayers.Class(null,
> {
>    map: null,
>    layer: null,
>    initialize: function() {
>    . . .
>    },
> 
>    CLASS_NAME: "BaseMap"
> });
> 
> 
> 
> What I'd like to be able to do us use a class name of
> 'com.mycompany.appname.BaseMap', but if I make those substitutions I get a
> 'com is not defined' error.

You have to define the namespaces before you can add a class to them:

var com = {
    mycompany: {
        appname: {}
    }
};
com.myCompany.appname.BaseMap = OpenLayers.Class(...);

Regards,
Andreas.

>  
> 
> Am I overlooking something basic?  Should I be using something other than
> the OL framework for JS classes?  We're also using jQuery for other things
> (DOM manipulation, etc.) and would be willing to use that if it makes more
> sense, but I'd rather not use yet another framework.
> 
> 
> -- 
> View this message in context: http://osgeo-org.1803224.n2.nabble.com/newbie-q-how-to-create-a-new-class-that-is-not-inherited-tp5502047p5502047.html
> Sent from the OpenLayers Dev mailing list archive at Nabble.com.
> _______________________________________________
> Dev mailing list
> Dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev



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



More information about the Dev mailing list