Handle unlistening in base class

This commit is contained in:
Tom Payne
2013-04-05 20:54:27 +02:00
parent 06bcab8374
commit 25eb18d2c8
3 changed files with 17 additions and 37 deletions

View File

@@ -48,10 +48,10 @@ ol.control.Control = function(controlOptions) {
this.map_ = null; this.map_ = null;
/** /**
* @private * @protected
* @type {?number} * @type {!Array.<?number>}
*/ */
this.postrenderListenKey_ = null; this.listenerKeys = [];
if (goog.isDef(controlOptions.map)) { if (goog.isDef(controlOptions.map)) {
this.setMap(controlOptions.map); this.setMap(controlOptions.map);
@@ -95,9 +95,9 @@ ol.control.Control.prototype.setMap = function(map) {
if (!goog.isNull(this.map_)) { if (!goog.isNull(this.map_)) {
goog.dom.removeNode(this.element); goog.dom.removeNode(this.element);
} }
if (!goog.isNull(this.postrenderListenKey_)) { if (!goog.array.isEmpty(this.listenerKeys)) {
goog.events.unlistenByKey(this.postrenderListenKey_); goog.array.forEach(this.listenerKeys, goog.events.unlistenByKey);
this.postrenderListenKey_ = null; this.listenerKeys.length = 0;
} }
this.map_ = map; this.map_ = map;
if (!goog.isNull(this.map_)) { if (!goog.isNull(this.map_)) {
@@ -105,8 +105,8 @@ ol.control.Control.prototype.setMap = function(map) {
this.target_ : map.getOverlayContainer(); this.target_ : map.getOverlayContainer();
goog.dom.appendChild(target, this.element); goog.dom.appendChild(target, this.element);
if (this.handleMapPostrender !== goog.nullFunction) { if (this.handleMapPostrender !== goog.nullFunction) {
this.postrenderListenKey_ = goog.events.listen(map, this.listenerKeys.push(goog.events.listen(map,
ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this); ol.MapEventType.POSTRENDER, this.handleMapPostrender, false, this));
} }
} }
}; };

View File

@@ -42,12 +42,6 @@ ol.control.DragBox = function(dragBoxOptions) {
*/ */
this.startCoordinate_ = dragBoxOptions.startCoordinate; this.startCoordinate_ = dragBoxOptions.startCoordinate;
/**
* @private
* @type {?number}
*/
this.dragListenKey_ = null;
goog.base(this, { goog.base(this, {
element: element, element: element,
map: dragBoxOptions.map map: dragBoxOptions.map
@@ -61,19 +55,15 @@ goog.inherits(ol.control.DragBox, ol.control.Control);
* @inheritDoc * @inheritDoc
*/ */
ol.control.DragBox.prototype.setMap = function(map) { ol.control.DragBox.prototype.setMap = function(map) {
if (!goog.isNull(this.dragListenKey_)) { goog.base(this, 'setMap', map);
goog.events.unlistenByKey(this.dragListenKey_);
this.dragListenKey_ = null;
}
if (!goog.isNull(map)) { if (!goog.isNull(map)) {
this.startPixel_ = map.getPixelFromCoordinate(this.startCoordinate_); this.startPixel_ = map.getPixelFromCoordinate(this.startCoordinate_);
goog.asserts.assert(goog.isDef(this.startPixel_)); goog.asserts.assert(goog.isDef(this.startPixel_));
goog.style.setPosition(this.element, this.startPixel_); goog.style.setPosition(this.element, this.startPixel_);
goog.style.setBorderBoxSize(this.element, new ol.Size(0, 0)); goog.style.setBorderBoxSize(this.element, new ol.Size(0, 0));
this.dragListenKey_ = goog.events.listen( this.listenerKeys.push(goog.events.listen(
map, ol.MapBrowserEvent.EventType.DRAG, this.updateBox_, false, this); map, ol.MapBrowserEvent.EventType.DRAG, this.updateBox_, false, this));
} }
goog.base(this, 'setMap', map);
}; };

View File

@@ -85,12 +85,6 @@ ol.control.MousePosition = function(opt_options) {
*/ */
this.lastMouseMovePixel_ = null; this.lastMouseMovePixel_ = null;
/**
* @private
* @type {Array.<?number>}
*/
this.listenerKeys_ = null;
}; };
goog.inherits(ol.control.MousePosition, ol.control.Control); goog.inherits(ol.control.MousePosition, ol.control.Control);
@@ -137,19 +131,15 @@ ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) {
* @inheritDoc * @inheritDoc
*/ */
ol.control.MousePosition.prototype.setMap = function(map) { ol.control.MousePosition.prototype.setMap = function(map) {
if (!goog.isNull(this.listenerKeys_)) {
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey);
this.listenerKeys_ = null;
}
goog.base(this, 'setMap', map); goog.base(this, 'setMap', map);
if (!goog.isNull(map)) { if (!goog.isNull(map)) {
var viewport = map.getViewport(); var viewport = map.getViewport();
this.listenerKeys_ = [ this.listenerKeys.push(
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE, goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
this.handleMouseMove, false, this), this.handleMouseMove, false, this),
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT, goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
this.handleMouseOut, false, this) this.handleMouseOut, false, this)
]; );
} }
}; };