Alternatively manage features in an ol.Collection
ol.layer.Vector can now manage both an RTree and a Collection of features. The new useSpatialIndex option allows to opt out of RTree management, and the new ol.Collection type of the features option allows to opt in for Collection management.
This commit is contained in:
@@ -427,10 +427,69 @@ describe('ol.source.Vector', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('with useSpatialIndex set to false', function() {
|
||||
var source;
|
||||
beforeEach(function() {
|
||||
source = new ol.source.Vector({useSpatialIndex: false});
|
||||
});
|
||||
|
||||
it('returns a features collection', function() {
|
||||
expect(source.getFeaturesCollection()).to.be.a(ol.Collection);
|
||||
});
|
||||
|
||||
it('#forEachFeatureInExtent loops through all features', function() {
|
||||
source.addFeatures([new ol.Feature(), new ol.Feature()]);
|
||||
var spy = sinon.spy();
|
||||
source.forEachFeatureInExtent([0, 0, 0, 0], spy);
|
||||
expect(spy.callCount).to.be(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with a collection of features', function() {
|
||||
var collection, source;
|
||||
beforeEach(function() {
|
||||
collection = new ol.Collection();
|
||||
source = new ol.source.Vector({
|
||||
features: collection
|
||||
});
|
||||
});
|
||||
|
||||
it('#getFeaturesCollection returns the configured collection', function() {
|
||||
expect(source.getFeaturesCollection()).to.equal(collection);
|
||||
});
|
||||
|
||||
it('keeps the collection in sync with the source\'s features', function() {
|
||||
var feature = new ol.Feature();
|
||||
source.addFeature(feature);
|
||||
expect(collection.getLength()).to.be(1);
|
||||
source.removeFeature(feature);
|
||||
expect(collection.getLength()).to.be(0);
|
||||
source.addFeatures([feature]);
|
||||
expect(collection.getLength()).to.be(1);
|
||||
source.clear();
|
||||
expect(collection.getLength()).to.be(0);
|
||||
});
|
||||
|
||||
it('keeps the source\'s features in sync with the collection', function() {
|
||||
var feature = new ol.Feature();
|
||||
collection.push(feature);
|
||||
expect(source.getFeatures().length).to.be(1);
|
||||
collection.remove(feature);
|
||||
expect(source.getFeatures().length).to.be(0);
|
||||
collection.extend([feature]);
|
||||
expect(source.getFeatures().length).to.be(1);
|
||||
collection.clear();
|
||||
expect(source.getFeatures().length).to.be(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
goog.require('goog.events');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.proj');
|
||||
|
||||
Reference in New Issue
Block a user