Allow source to be set with set method as well
This commit is contained in:
@@ -4,6 +4,7 @@ goog.require('goog.asserts');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.layer.Base');
|
||||
goog.require('ol.layer.LayerProperty');
|
||||
|
||||
@@ -37,6 +38,10 @@ ol.layer.Layer = function(options) {
|
||||
*/
|
||||
this.sourceChangeKey_ = null;
|
||||
|
||||
goog.events.listen(this,
|
||||
ol.Object.getChangeEventType(ol.layer.LayerProperty.SOURCE),
|
||||
this.handleSourcePropertyChange_, false, this);
|
||||
|
||||
this.setSource(options.source);
|
||||
};
|
||||
goog.inherits(ol.layer.Layer, ol.layer.Base);
|
||||
@@ -109,6 +114,23 @@ ol.layer.Layer.prototype.handleSourceChange_ = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ol.layer.Layer.prototype.handleSourcePropertyChange_ = function() {
|
||||
if (!goog.isNull(this.sourceChangeKey_)) {
|
||||
goog.events.unlistenByKey(this.sourceChangeKey_);
|
||||
this.sourceChangeKey_ = null;
|
||||
}
|
||||
var source = this.getSource();
|
||||
if (!goog.isNull(source)) {
|
||||
this.sourceChangeKey_ = goog.events.listen(source,
|
||||
goog.events.EventType.CHANGE, this.handleSourceChange_, false, this);
|
||||
}
|
||||
this.changed();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the layer source.
|
||||
* @param {ol.source.Source} source The layer source.
|
||||
@@ -116,14 +138,7 @@ ol.layer.Layer.prototype.handleSourceChange_ = function() {
|
||||
* @api stable
|
||||
*/
|
||||
ol.layer.Layer.prototype.setSource = function(source) {
|
||||
if (!goog.isNull(this.sourceChangeKey_)) {
|
||||
goog.events.unlistenByKey(this.sourceChangeKey_);
|
||||
this.sourceChangeKey_ = null;
|
||||
}
|
||||
this.sourceChangeKey_ = goog.events.listen(source,
|
||||
goog.events.EventType.CHANGE, this.handleSourceChange_, false, this);
|
||||
this.set(ol.layer.LayerProperty.SOURCE, source);
|
||||
this.changed();
|
||||
};
|
||||
goog.exportProperty(
|
||||
ol.layer.Layer.prototype,
|
||||
|
||||
@@ -253,6 +253,52 @@ describe('ol.layer.Layer', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#set("source", source)', function() {
|
||||
var projection = ol.proj.get('EPSG:4326');
|
||||
|
||||
it('sets the layer source', function() {
|
||||
var layer = new ol.layer.Layer({
|
||||
source: new ol.source.Source({projection: projection})
|
||||
});
|
||||
|
||||
var source = new ol.source.Source({projection: projection});
|
||||
layer.set('source', source);
|
||||
expect(layer.getSource()).to.be(source);
|
||||
});
|
||||
|
||||
it('calls changed', function() {
|
||||
var layer = new ol.layer.Layer({
|
||||
source: new ol.source.Source({projection: projection})
|
||||
});
|
||||
sinon.spy(layer, 'changed');
|
||||
|
||||
var source = new ol.source.Source({projection: projection});
|
||||
layer.set('source', source);
|
||||
expect(layer.changed.calledOnce).to.be(true);
|
||||
});
|
||||
|
||||
it('sets up event listeners', function() {
|
||||
sinon.spy(ol.layer.Layer.prototype, 'handleSourceChange_');
|
||||
|
||||
var first = new ol.source.Source({projection: projection});
|
||||
var layer = new ol.layer.Layer({source: first});
|
||||
|
||||
first.setState(ol.source.State.READY);
|
||||
expect(layer.handleSourceChange_.calledOnce).to.be(true);
|
||||
|
||||
var second = new ol.source.Source({projection: projection});
|
||||
layer.set('source', second);
|
||||
|
||||
expect(layer.handleSourceChange_.calledOnce).to.be(true);
|
||||
second.setState(ol.source.State.READY);
|
||||
expect(layer.handleSourceChange_.callCount).to.be(2);
|
||||
|
||||
// remove spy
|
||||
ol.layer.Layer.prototype.handleSourceChange_.restore();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#setSource()', function() {
|
||||
var projection = ol.proj.get('EPSG:4326');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user