[Users] How to find layer record from layer node ?

Tim Schaub tschaub at opengeo.org
Wed Mar 24 20:19:14 CET 2010


Alexandre Dube wrote:
> Tim,
> 
>  Someone on the Ext irc channel proposed using the following user-custom 
> method instead :
> 
>    /**
>     * Returns all records that absolutely match the field value. Ext's 
> query
>     * function uses a regular expression instead, which is just a terrible
>     * idea.
>     * @param field {String} The field to check
>     * @param val The value to match
>     * @return A set of the matching records
>     */
>    realQuery: function(field, val) {
>        return this.queryBy(function(rec) {
>            return rec.get(field) === val;
>        });
>    },
> 
> Since it's not an official Ext method, I would put it in the LayerStore 
> class.  What do you think ?
> 

See http://www.extjs.com/forum/showthread.php?p=450523#post450523 for my 
suggestion.

If anything, I think we would add store.queryExact.  However, since you 
can rather trivially get what you need from queryBy, the need for a new 
method is pretty low.  The drawback with adding convenience methods like 
these is that when Ext does the same, we are stuck maintaining redundant 
code (at best with a different name, at worst with the same name but 
different behavior or signature).

Tim


> Alexandre
> 
> 
> Alexandre Dube wrote:
>> Tim,
>>
>> Tim Schaub wrote:
>>  
>>> In these cases, I think it is easiest to think of the layer store 
>>> referenced by the MapPanel as the primary store.  The the map is a 
>>> view of the records in this store.  The tree (in this case) can also 
>>> be treated as a view.  A grid could be another view.  To keep things 
>>> simple, don't call methods on the views.
>>>
>>> Tim
>>>       
>> That's perfectly clear. The below line is working well and, as you 
>> mentioned, it removed the layer from all views :
>>
>> node.layerStore.remove(node.layerStore.getAt(1));
>>
>> One last question on the matter though : it's about how I can get the 
>> index of the record I'm looking for. In OpenLayers, since the layers 
>> are stored into an array, using OpenLayers.indexOf does the trick, but 
>> is there something similar in Ext or GeoExt ? Is there a "find record 
>> by value" method ?
>>
>> Here's how I would do it : create a method for the Store object that 
>> creates an array with values of a specific attribute. The result would 
>> be similar to the layers array in OpenLayers, so from then we would 
>> simply need to use indexOf. What do you think ?
>>
>>
>> Ext.data.Store.prototype.getValueArray = function(attribute) {
>> var values = [];
>> var nValues = this.getCount();
>>
>> for(var i=0; i<nValues; i++) {
>> values.push(this.getAt(i).get(attribute));
>> }
>>
>> return values;
>> };
>>
>> var layers = node.layerStore.getValueArray('layer');
>> var index = OpenLayers.Util.indexOf(layers, node.layer);
>> if(index == -1) {
>> node.layerStore.remove(node.layerStore.getAt(index));
>> }
>>
>> Regards,
>>
>>   
> 
> 


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


More information about the Users mailing list