[OpenLayers-Dev] [strk@keybit.net: [OpenLayers-Users] Style context as an object ?]

Tim Schaub tschaub at opengeo.org
Tue Jul 6 16:46:58 EDT 2010


On 7/6/10 2:47 AM, strk wrote:
> Guys, I'm forwarding this as I think what I'm trying to do
> is limited by actual OL code.
>
> Basically I'm trying to use an "instance object" as the
> 'context' for a Style.
>

If you want to use the "this" keyword in methods on the context object, 
you have two options:

1) Bind the methods explicitly (untested code).

     var context = {
         foo: function() {
             return this.works;
         },
         works: true
     };
     for (var key in context) {
         var val = context[key];
         if (typeof val === "function") {
             context[key] = OpenLayers.Function.bind(val, context);
         }
     }

This should allow the following to work:

     var method = context.foo;
     method();

2) Submit a patch for BaseTypes.js that changes the string format method 
so that if the replacement is a function, the following is run:

     replacement.apply(context, args || []);


For OL3, I'd like to see us do away with the context stuff in Style. 
I'd prefer to see us using a beforerender event instead.  If this were 
triggered by the layer before rendering, the same functionality could be 
achieved and we could optimize for cases where there are no listeners 
for beforerender (and no expressions in symbolizers).

Tim

> The idea is to have generic style handlers exposed in the
> drupal OpenLayers module. The genericity would be obtained
> by defining classes which take parameters in the constructor
> and expose methods basing their working on the passed parameters.
>
> Example code is in the forwarded message.
> The actual code I'm using is tested to work in a normal
> (out of OpenLayers) context, that is I can instanciate
> the class and access the parameters passed by the constructor
> trough 'this' from the methods.
>
> Problem is, when attaching the instance in the 'context'
> property of a Style, later call to the methods don't
> resolve 'this' to the object itself, but to the top
> of DOM.
>
> Can this be due to the Style.clone method ?
> That method (lib/OpenLayers/Style.js) does:
>
>          // clone context
>          options.context = this.context&&  OpenLayers.Util.extend({}, this.context);
>
> Would the OpenLayers.Util.extent call change the nature
> of the context object ?
> If so, is there anything required on the context class itself
> to retain the nature ? (the reference to 'this') ?
> Or, do you have other suggestions for workarounds ?
>
> Thanks in advance.
>
> --strk;
>
> ----- Forwarded message from strk<strk at keybit.net>  -----
>
> Date: Mon, 5 Jul 2010 17:41:51 +0200
> From: strk<strk at keybit.net>
> Subject: [OpenLayers-Users] Style context as an object ?
> To: users at openlayers.org
> X-BeenThere: users at openlayers.org
> List-Id: OpenLayers User discussion<users.openlayers.org>
>
> Did anyone ever tried using a "proper" object
> as a Style context ?
>
> What I'm trying to do is create a class of style
> contexts and pass instances of them (for parametrized
> styles) as a Style.context, but 'this', within the
> callbacks, seem to always evaluate to the top of DOM.
>
> Example code:
>
>    MyContextClass = function(radius) {
>    	this.radius = radius;
>    };
>
>    MyContextClass.prototype.getRadius() {
>    	alert(this.radius);
>    };
>
>    myContext = new MyContextClass(4);
>
>    myStyle = OpenLayers.Style(
>    	{ pointRadius: '${getRadius)' },
>    	{ context: myContext }
>    );
>
> --strk;
>
>    ()   Free GIS&  Flash consultant/developer
>    /\   http://strk.keybit.net/services.html
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
> ----- End forwarded message -----
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Dev mailing list