[OpenLayers-Dev] createLiteral enhancement proposal

Paul Spencer pspencer at dmsolutions.ca
Sun Jun 21 12:54:15 EDT 2009


Thanks for commenting, Andreas ...

On 21-Jun-09, at 12:29 PM, Andreas Hocevar wrote:

> 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.

I thought of this and then decided on passing an array in the (perhaps  
unlikely) event that someone else thought of a need for other  
arguments they would like passed through createLiteral to  
String.format and beyond.  If that won't happen, then passing a fourth  
argument is probably the safer change (even though it is not an  
APIMethod)feat.

I will propose your suggestion as the patch in trac with a comment  
about my alternative and the possible reasons to implement one over  
the other.  I'll add it to http://trac.openlayers.org/ticket/2146  
which proposes to modify createLiterals to combine the context,  
attributes and data.

>
> Regards,
> Andreas.
>


__________________________________________

    Paul Spencer
    Chief Technology Officer
    DM Solutions Group Inc
    http://research.dmsolutions.ca/




More information about the Dev mailing list