Re-render on feature changes

This commit is contained in:
Éric Lemoine
2014-03-10 09:47:09 +01:00
parent 8c9d22c5e5
commit 814e5d2790
3 changed files with 21 additions and 5 deletions
-1
View File
@@ -149,7 +149,6 @@ ol.Feature.prototype.handleGeometryChanged_ = function() {
this.geometryChangeKey_ = goog.events.listen(geometry, this.geometryChangeKey_ = goog.events.listen(geometry,
goog.events.EventType.CHANGE, this.handleGeometryChange_, false, this); goog.events.EventType.CHANGE, this.handleGeometryChange_, false, this);
} }
this.dispatchChangeEvent();
}; };
+12 -4
View File
@@ -12,6 +12,7 @@ goog.require('goog.events');
goog.require('goog.events.Event'); goog.require('goog.events.Event');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.ObjectEventType');
goog.require('ol.source.Source'); goog.require('ol.source.Source');
goog.require('ol.structs.RBush'); goog.require('ol.structs.RBush');
@@ -58,7 +59,7 @@ ol.source.Vector = function(opt_options) {
/** /**
* @private * @private
* @type {Object.<string, goog.events.Key>} * @type {Object.<string, Array.<goog.events.Key>>}
*/ */
this.featureChangeKeys_ = {}; this.featureChangeKeys_ = {};
@@ -88,8 +89,14 @@ ol.source.Vector.prototype.addFeature = function(feature) {
ol.source.Vector.prototype.addFeatureInternal = function(feature) { ol.source.Vector.prototype.addFeatureInternal = function(feature) {
var featureKey = goog.getUid(feature).toString(); var featureKey = goog.getUid(feature).toString();
goog.asserts.assert(!(featureKey in this.featureChangeKeys_)); goog.asserts.assert(!(featureKey in this.featureChangeKeys_));
this.featureChangeKeys_[featureKey] = goog.events.listen(feature, this.featureChangeKeys_[featureKey] = [
goog.events.EventType.CHANGE, this.handleFeatureChange_, false, this); goog.events.listen(feature,
goog.events.EventType.CHANGE,
this.handleFeatureChange_, false, this),
goog.events.listen(feature,
ol.ObjectEventType.PROPERTYCHANGE,
this.handleFeatureChange_, false, this)
];
var geometry = feature.getGeometry(); var geometry = feature.getGeometry();
if (goog.isNull(geometry)) { if (goog.isNull(geometry)) {
this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature; this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature;
@@ -343,7 +350,8 @@ ol.source.Vector.prototype.removeFeature = function(feature) {
ol.source.Vector.prototype.removeFeatureInternal = function(feature) { ol.source.Vector.prototype.removeFeatureInternal = function(feature) {
var featureKey = goog.getUid(feature).toString(); var featureKey = goog.getUid(feature).toString();
goog.asserts.assert(featureKey in this.featureChangeKeys_); goog.asserts.assert(featureKey in this.featureChangeKeys_);
goog.events.unlistenByKey(this.featureChangeKeys_[featureKey]); goog.array.forEach(this.featureChangeKeys_[featureKey],
goog.events.unlistenByKey);
delete this.featureChangeKeys_[featureKey]; delete this.featureChangeKeys_[featureKey];
this.dispatchEvent(new ol.source.VectorEvent( this.dispatchEvent(new ol.source.VectorEvent(
ol.source.VectorEventType.REMOVEFEATURE, feature)); ol.source.VectorEventType.REMOVEFEATURE, feature));
+9
View File
@@ -233,6 +233,15 @@ describe('ol.source.Vector', function() {
expect(vectorSource.getFeatures()).to.eql([feature]); expect(vectorSource.getFeatures()).to.eql([feature]);
}); });
it('fires a change event when setting a feature\'s property', function() {
var feature = new ol.Feature(new ol.geom.Point([1, 1]));
vectorSource.addFeature(feature);
var listener = sinon.spy();
goog.events.listen(vectorSource, 'change', listener);
feature.set('foo', 'bar');
expect(listener).to.be.called();
});
}); });
}); });