Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for retrieving features by id #2087

Merged
merged 3 commits into from
May 23, 2014
Merged

Add method for retrieving features by id #2087

merged 3 commits into from
May 23, 2014

Conversation

tschaub
Copy link
Member

@tschaub tschaub commented May 20, 2014

This adds a feature id index to the vector source. The getFeatureById method allows retrieval of features by id.

Note that the index does not differentiate string and numeric identifiers. So source.getFeatureById(2) and source.getFeatureById('2') both would return a feature with an id of 2 or '2'. This limitation should not affect applications that consistently use either strings or numbers as feature identifiers.

Fixes #2084.

expect(feature.getId()).to.be('foo');
done();
});
feature.setId('foo');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this test will pass, even if no change event is dispatched.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails after the 2000ms timeout (done is never called).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But using done makes more sense for async call no? In that case wouldn't it make more sense to use a spy?

    it('dispatches the "change" event', function() {
      var spy = sinon.spy();
      var feature = new ol.Feature();
      feature.on('feature', spy);
      feature.setId('foo');
      expect(spy.calledOnce).to.be(true);
      expect(feature.getId()).to.be('foo');
    });

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that is asserting two things at once: that the change event was fired and that it was fired synchronously. I'm partial to the idea of having change events fired asynchronously, and the tests reveal this bias.

@tschaub
Copy link
Member Author

tschaub commented May 21, 2014

This is ready for review.

@tschaub
Copy link
Member Author

tschaub commented May 21, 2014

As noted above the tests do fail without the lib changes.

@twpayne
Copy link
Contributor

twpayne commented May 21, 2014

OK, I just wonder if:

var feature = new ol.Feature();
var callbackCalled = false;
feature.on('change', function() {
  callbackCalled = true;
});
feature.setId('foo');
expect(callbackCalled).to.be(true);

would be simpler and faster.

@tschaub
Copy link
Member Author

tschaub commented May 21, 2014

That wouldn't pass any faster. But I grant that it would fail faster.

It is a pretty common pattern to call the done callback within an event listener (even within this same spec file). I'm surprised this one is drawing attention.

Perhaps we could separately go through the tests and modify things so we are essentially asserting that certain events are fired synchronously. It would be more consistent to do that as a separate effort though.

if (goog.isDef(id)) {
goog.asserts.assert(!(String(id) in this.idIndex_),
'Feature with same id already added to the source: ' + id);
this.idIndex_[String(id)] = feature;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor but we use toString a lot elsewhere. AFAICT toString works on both numbers and strings.

@elemoine
Copy link
Member

It looks good to me. I just added a minor, non-blocking, comments. Thanks.

@tschaub
Copy link
Member Author

tschaub commented May 23, 2014

I'll rebase to get tests passing after #2017 is merged.

@elemoine
Copy link
Member

Please merge when rebased (and when the build passes).

tschaub added a commit that referenced this pull request May 23, 2014
Add method for retrieving features by id.
@tschaub tschaub merged commit 92d8dd9 into openlayers:master May 23, 2014
@tschaub tschaub deleted the id-index branch May 23, 2014 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add feature id index to vector source
3 participants