[OpenLayers-Dev] createLiteral enhancement proposal

Andreas Hocevar ahocevar at opengeo.org
Sun Jun 21 12:29:08 EDT 2009


Hi Paul,

On Fri, Jun 19, 2009 at 5:14 PM, Paul Spencer<pspencer at dmsolutions.ca> wrote:
> In the context of calling functions to compute styles for features
> using the ${functionName} syntax, it would be very useful for me if
> the property being computed was passed to functionName() in addition
> to the feature itself.  I've tested this locally by modifying
> createLiterals to pass an array with [feature,i] (where i is the
> property name) and createLiteral to replace feature with args, which
> is passed to String.format as the third value.  This works perfectly
> for me.  The only other use of createLiteral is in Rule, which does
> not use the third argument at all.
>
> I would like to propose this change as a patch but I am undecided if
> the appropriate change is to pass an array as the third argument to
> createLiteral or to test in createLiteral if the third argument is an
> array or not and modify it appropriately for passing on to
> String.format ...
>
> Currently, I am using this:
>
> OpenLayers.Style.createLiteral = function(value, context, args) {
>     if (typeof value == "string" && value.indexOf("${") != -1) {
>         value = OpenLayers.String.format(value, context, args);
>         value = (isNaN(value) || !value) ? value : parseFloat(value);
>     }
>     return value;
> };

What about

OpenLayers.Style.createLiteral = function(value, context, feature, property) {
    if (typeof value == "string" && value.indexOf("${") != -1) {
        value = OpenLayers.String.format(value, context, [feature, property]);
        value = (isNaN(value) || !value) ? value : parseFloat(value);
    }
    return value;
};

Then you could have

    createLiterals: function(style, feature) {
        var context = this.context || feature.attributes || feature.data;

        for (var i in this.propertyStyles) {
            style[i] = OpenLayers.Style.createLiteral(style[i],
context, feature, i);
        }
        return style;
    },

and would not have to worry about API changes.

Regards,
Andreas.


>
> Comments?
>
> __________________________________________
>
>    Paul Spencer
>    Chief Technology Officer
>    DM Solutions Group Inc
>    http://research.dmsolutions.ca/
>
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev
>



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



More information about the Dev mailing list