Ticket #2549 (closed bug: fixed)

Opened 3 years ago

Last modified 3 years ago

Layer.Vector fires featuresadded event with shapes vetoed by beforefeatureadded

Reported by: timbonicus Owned by: crschmidt
Priority: minor Milestone: 2.10 Release
Component: Layer.Vector Version: 2.8
Keywords: beforefeatureadded featuresadded Cc:
State: Complete

Description

In OpenLayers.Layer.Vector#addFeatures, the beforefeatureadded event is fired allowing listeners to return false and veto adding the feature to the layer. The feature is still added to this.features in the class before this opportunity and fired with the featuresadded event at the end of the function.

The attached patch corrects this behavior and never adds the feature to this.features or fires it with the featuresadded event if the shape was vetoed.

Attachments

beforefeatureadded.patch Download (0.9 KB) - added by timbonicus 3 years ago.
beforefeatureadded2.patch Download (0.9 KB) - added by timbonicus 3 years ago.
patch using splice instead of this.features
beforefeatureadded3.patch Download (1.6 KB) - added by timbonicus 3 years ago.
new patch cloning features and splicing safely
beforefeatureadded4.patch Download (3.4 KB) - added by timbonicus 3 years ago.
patch with included unit test

Change History

Changed 3 years ago by timbonicus

  Changed 3 years ago by elemoine

  • state set to Needs More Work

Moving this.features.push(feature) after beforefeatureadded is triggered makes perfect sense to me. But I disagree with sending this.features instead of features with the featuresadded event.

  Changed 3 years ago by timbonicus

The features array will contain all features, this.features will contain only the features that were actually added after firing the beforefeatureadded events. Without the second change, featuresadded will fire with potentially extraneous features that were not added.

  Changed 3 years ago by timbonicus

I see what you mean that this.features may contain other features that were added at an earlier time. It seems like features that were not added after firing the beforefeatureadded event need to be spliced from the features array.

Changed 3 years ago by timbonicus

patch using splice instead of this.features

  Changed 3 years ago by elemoine

Comments on beforefeatureadded2.patch Download:

to my knowledge the slice method does not modify the array it's applied to. So I think we should introduce a local variable addedFeatures, initially referencing this.features, and set to features.slice(i, 1) when triggerEvent("beforefeatureadded") returns false. What do you think?

  Changed 3 years ago by elemoine

  • type changed from feature to bug

  Changed 3 years ago by timbonicus

slice does not modify the original array; splice is a different function that does modify the original array. There is a problem with the second patch in that splice will concurrently modify the array we are iterating over. In this sense, your solution of having a copy of the array is much safer/preferable. I am attaching a new patch with this modification. See what you think.

Changed 3 years ago by timbonicus

new patch cloning features and splicing safely

  Changed 3 years ago by elemoine

timbonicus, sorry for mixing up slice and splice, and thanks for beforefeatureadded3.patch Download, it looks good to me. It'd be great if you could check that the tests pass (at least in FF3), and add a test for this new behavior/correction.

Also, do we have a ICLA or CCLA from you already? I don't see you in the list at:  http://trac.openlayers.org/wiki/CLA. See:  http://trac.openlayers.org/wiki/HowToContribute for more details.

Thanks,

follow-up: ↓ 9   Changed 3 years ago by timbonicus

The tests run without issue, I'm adding another patch with a test for this functionality included. I switched the behavior to just use Array.push instead of the more confusing splice.

Thanks for pointing out the CLA, our CTO faxed in a CCLA this morning.

Changed 3 years ago by timbonicus

patch with included unit test

in reply to: ↑ 8 ; follow-up: ↓ 10   Changed 3 years ago by elemoine

Replying to timbonicus:

The tests run without issue, I'm adding another patch with a test for this functionality included. I switched the behavior to just use Array.push instead of the more confusing splice. Thanks for pointing out the CLA, our CTO faxed in a CCLA this morning.

Is your company now listed in < http://trac.openlayers.org/wiki/CLA>? Which one is it? Chris, have you received this CCLA and added to the list?

in reply to: ↑ 9 ; follow-up: ↓ 11   Changed 3 years ago by timbonicus

Replying to elemoine:

Is your company now listed in < http://trac.openlayers.org/wiki/CLA>? Which one is it? Chris, have you received this CCLA and added to the list?

The company is RealGo, it does not appear on that list as of yet.

in reply to: ↑ 10   Changed 3 years ago by elemoine

Replying to timbonicus:

Replying to elemoine:

Is your company now listed in < http://trac.openlayers.org/wiki/CLA>? Which one is it? Chris, have you received this CCLA and added to the list?

The company is RealGo, it does not appear on that list as of yet.

Chris, have we received a fax from RealGo? Thanks.

  Changed 3 years ago by elemoine

  • state changed from Needs More Work to Commit

Code looks good. Tests pass in FF3, Chromium5 and IE7. And we have the CCLA from RealGo. Thanks for patch timbonicus.

  Changed 3 years ago by tschaub

  • status changed from new to closed
  • state changed from Commit to Complete
  • resolution set to fixed

Closed with r2549.

Note: See TracTickets for help on using tickets.