[OpenLayers-Dev] DeleteFeature control

Eric Lemoine eric.lemoine at camptocamp.com
Tue Jul 21 14:20:16 EDT 2009


On Tuesday, July 21, 2009, Alexandre Dube <adube at mapgears.com> wrote:
> Hi,
>
>  I'm trying to reproduce what we discuss.  I'm not really familiar with this method and I'm having difficulties with 'this'.  Here's what I have so far :
>
> /*** START ***/
> var delCtrl = new OpenLayers.Control({
>    layer: olWFSRoads,
>      handlers: {"keyboard": new

either use "handler" or give "handlers" an array

OpenLayers.Handler.Keyboard(this,{ // <------- 'this' is not the control

indeed, it's not the control, it is the current scope/object. Here
it's the object that's building the control, it may be "window" if
you're not creating the control from a specific object


>            keydown: function(evt) {
>            var code = evt.keyCode;
>            //var features = this.layer.selectedFeatures; // <------- this is not the control, and so on...

yet another context, "this" refers to the keyboard handler here
("keydown" is a method of the keyboard handler)

>            var features = olWFSRoads.selectedFeatures; // <----- can we avoid the use of olWFSRoads ?

I'm not sure whethet you can give handlers a specific scope for
executing callbacks. I don't think so.

>
>            if(code == OpenLayers.Event.KEY_DELETE && features.length > 0 &&
>               this.confirmDelete() ) {
>                this.deleteFeatures(features);
>                return;
>            }
>        }
>      })},
>    controls: [
>        new OpenLayers.Control.SelectFeature(
>            olWFSRoads, {
>                onSelect: function(f) {
>                    f.state = OpenLayers.State.DELETE;
>                    this.layer.drawFeature(f);
>                }
>        })
>    ],
>    confirmDelete: function() {
>        return (confirm("Delete selected features ?"))
>    },
>    deleteFeatures: function(features) {
>        this.layer.destroyFeatures(features);
>    }
> });
> /*** END ***/
>
> I made the discussed changes to the OL.Control to be able to automatize activation/deactivation/setMap.
>
> If you look at the <---------- arrow, you'll see a 'this', which is supposed to be the control, owner of the handler.  Well, that 'this' is not the control, it's the html page (figures).  I tried to put 'delCtrl' instead but it didn't work.  I really wonder how I am supposed to create controls that way (without creating a class) and use the 'this' (or something else).
>
> Any hint would be greatly appreciated,
>
> Alexandre
>
> P.S. Also, I believe it's ok to have my 'user' functions, like confirmDelete directly inside the control since the control is completely custom-made (i.e. not using events).
>
> Alexandre Dube wrote:
>
> Eric Lemoine wrote:
>
>
> so, to get to the delete control you've been working on, I see more
> value, in terms of flexibility, in adding "control composition" to the
> control base class.
>
>
>
>
>
> I also agree.  Originally, the control had a Feature handler instead of a SelectFeature control.  Looking at it now, it's too simple to be justified as a new control.
>
>
>
> About the 'controls' property and the act./dea. automatism : that's a good idea, but wouldn't it be good to have a 'handlers' property also and activate/deactivate all handlers as well ?  Currently, control has a 'handler' property only.  Would it make sense to have 'handlers' added a do the same kind of automatism as the new 'controls' property ?
>
>
> Agreed.
>
>
>
>
> Great.
>
>
>
>
>
>
> --
> Alexandre Dubé
> Mapgears
> www.mapgears.com
>
>

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com



More information about the Dev mailing list