Skip to content

Commit

Permalink
Merge pull request openlayers#2323 from pagameba/fix-addfeatures
Browse files Browse the repository at this point in the history
ServerVector addFeaturesInternal fails when features lack an id
  • Loading branch information
pagameba committed Jul 10, 2014
2 parents 9640a08 + a02d21d commit 9dc1723
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ol/source/servervectorsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ ol.source.ServerVector.prototype.addFeaturesInternal = function(features) {
for (i = 0, ii = features.length; i < ii; ++i) {
var feature = features[i];
var featureId = feature.getId();
if (!(featureId in this.loadedFeatures_)) {
if (!goog.isDef(featureId)) {
notLoadedFeatures.push(feature);
} else if (!(featureId in this.loadedFeatures_)) {
notLoadedFeatures.push(feature);
this.loadedFeatures_[featureId] = true;
}
Expand Down
68 changes: 68 additions & 0 deletions test/spec/ol/source/servervectorsource.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
goog.provide('ol.test.source.ServerVector');


describe('ol.source.ServerVector', function() {

describe('when empty', function() {

var vectorSource;
beforeEach(function() {
vectorSource = new ol.source.ServerVector({});
});

describe('#addFeatures', function() {

it('adds features with the same id only once', function() {
var addfeatureSpy = sinon.spy();
vectorSource.on('addfeature', addfeatureSpy);
features = [];
var i;
var feature;
for (i = 0; i < 5; i++) {
feature = new ol.Feature();
feature.setId(0);
features.push(feature);
}
vectorSource.addFeatures(features);
expect(vectorSource.getFeatures().length).to.be(1);
expect(addfeatureSpy.callCount).to.be(1);
});

it('adds features all features with distinct ids', function() {
var addfeatureSpy = sinon.spy();
vectorSource.on('addfeature', addfeatureSpy);
features = [];
var i;
var feature;
for (i = 0; i < 5; i++) {
feature = new ol.Feature();
feature.setId(i);
features.push(feature);
}
vectorSource.addFeatures(features);
expect(vectorSource.getFeatures().length).to.be(5);
expect(addfeatureSpy.callCount).to.be(5);
});

it('adds features without ids', function() {
var addfeatureSpy = sinon.spy();
vectorSource.on('addfeature', addfeatureSpy);
features = [];
var i;
for (i = 0; i < 10; i++) {
features.push(new ol.Feature());
}
vectorSource.addFeatures(features);
expect(vectorSource.getFeatures().length).to.be(10);
expect(addfeatureSpy.callCount).to.be(10);
});

});

});

});


goog.require('ol.Feature');
goog.require('ol.source.ServerVector');

0 comments on commit 9dc1723

Please sign in to comment.