Do not implement a specific clone method

What we need here is a mix of deep and shallow cloning, and we
do not want to do this in a generic ol.Feature#clone() method.
This commit is contained in:
ahocevar
2013-08-28 19:18:17 +02:00
parent 2394b39f6f
commit 0c1f2328f9
3 changed files with 6 additions and 35 deletions

View File

@@ -51,20 +51,6 @@ ol.Feature = function(opt_values) {
goog.inherits(ol.Feature, ol.Object);
/**
* Create a clone of this feature. Both the feature and its geometry will be
* cloned.
* @return {ol.Feature} A clone of this feature, with a cloned geometry.
*/
ol.Feature.prototype.clone = function() {
var clone = new ol.Feature(this.getAttributes());
clone.setGeometry(this.getGeometry().clone());
clone.featureId_ = this.featureId_;
clone.symbolizers_ = this.symbolizers_;
return clone;
};
/**
* Gets a copy of the attributes of this feature.
* @return {Object.<string, *>} Attributes object.
@@ -130,7 +116,7 @@ ol.Feature.prototype.set = function(key, value) {
* Set the feature's commonly used identifier. This identifier is usually the
* unique id in the source store.
*
* @param {string} featureId The feature's identifier.
* @param {string|undefined} featureId The feature's identifier.
*/
ol.Feature.prototype.setFeatureId = function(featureId) {
this.featureId_ = featureId;

View File

@@ -162,9 +162,12 @@ ol.interaction.Select.prototype.select =
featuresToRemove.push(clone);
delete featureMap[featureId];
} else if (!(featureId in oldFeatureMap)) {
clone = feature.clone();
featureMap[featureId] = clone;
clone = new ol.Feature(feature.getAttributes());
clone.setGeometry(feature.getGeometry().clone());
clone.setFeatureId(feature.getFeatureId());
clone.setSymbolizers(feature.getSymbolizers());
clone.renderIntent = ol.layer.VectorLayerRenderIntent.SELECTED;
featureMap[featureId] = clone;
selectedFeatures.push(feature);
featuresToAdd.push(clone);
}

View File

@@ -34,24 +34,6 @@ describe('ol.Feature', function() {
});
describe('#clone()', function() {
it('creates a clone with a cloned geometry', function() {
var feature = new ol.Feature({
loc: new ol.geom.Point([10, 20]),
foo: 'bar'
});
feature.setFeatureId('foo');
var clone = feature.clone();
expect(clone).to.not.be(feature);
expect(clone.get('foo')).to.be('bar');
expect(clone.getFeatureId()).to.be('foo');
expect(clone.getGeometry()).to.not.be(feature.getGeometry());
expect(clone.getGeometry().getCoordinates()).to.eql([10, 20]);
});
});
describe('#get()', function() {
it('returns values set at construction', function() {