-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
expect(feature.getId()).to.be('foo'); | ||
done(); | ||
}); | ||
feature.setId('foo'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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');
});
There was a problem hiding this comment.
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.
This is ready for review. |
As noted above the tests do fail without the lib changes. |
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. |
That wouldn't pass any faster. But I grant that it would fail faster. It is a pretty common pattern to call the 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; |
There was a problem hiding this comment.
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.
It looks good to me. I just added a minor, non-blocking, comments. Thanks. |
I'll rebase to get tests passing after #2017 is merged. |
Please merge when rebased (and when the build passes). |
Add method for retrieving features by id.
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)
andsource.getFeatureById('2')
both would return a feature with an id of2
or'2'
. This limitation should not affect applications that consistently use either strings or numbers as feature identifiers.Fixes #2084.