Add ol.Object.unset() method
Allow deleting a property.
This commit is contained in:
@@ -131,7 +131,7 @@ ol.ObjectAccessor.prototype.transform = function(from, to) {
|
|||||||
* property is observable as well as the object as a whole.
|
* property is observable as well as the object as a whole.
|
||||||
*
|
*
|
||||||
* Classes that inherit from this have pre-defined properties, to which you can
|
* Classes that inherit from this have pre-defined properties, to which you can
|
||||||
* add your own. The pre-defined properties are listed in this documentation as
|
* add your owns. The pre-defined properties are listed in this documentation as
|
||||||
* 'Observable Properties', and have their own accessors; for example,
|
* 'Observable Properties', and have their own accessors; for example,
|
||||||
* {@link ol.Map} has a `target` property, accessed with `getTarget()` and
|
* {@link ol.Map} has a `target` property, accessed with `getTarget()` and
|
||||||
* changed with `setTarget()`. Not all properties are however settable. There
|
* changed with `setTarget()`. Not all properties are however settable. There
|
||||||
@@ -163,6 +163,9 @@ ol.ObjectAccessor.prototype.transform = function(from, to) {
|
|||||||
* details, and see {@link ol.dom.Input} for the specific case of binding an
|
* details, and see {@link ol.dom.Input} for the specific case of binding an
|
||||||
* object with an HTML element.
|
* object with an HTML element.
|
||||||
*
|
*
|
||||||
|
* Properties can be deleted by using the unset method. E.g.
|
||||||
|
* object.unset('foo').
|
||||||
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
* @extends {ol.Observable}
|
* @extends {ol.Observable}
|
||||||
* @param {Object.<string, *>=} opt_values An object with key-value pairs.
|
* @param {Object.<string, *>=} opt_values An object with key-value pairs.
|
||||||
@@ -489,3 +492,17 @@ ol.Object.prototype.unbindAll = function() {
|
|||||||
this.unbind(key);
|
this.unbind(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsets a property.
|
||||||
|
* @param {string} key Key name.
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
ol.Object.prototype.unset = function(key) {
|
||||||
|
if (key in this.values_) {
|
||||||
|
var oldValue = this.values_[key];
|
||||||
|
delete this.values_[key];
|
||||||
|
this.notify(key, oldValue);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ describe('ol.Object', function() {
|
|||||||
o = new ol.Object();
|
o = new ol.Object();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('get and set', function() {
|
describe('get, set and unset', function() {
|
||||||
|
|
||||||
describe('get an unset property', function() {
|
describe('get an unset property', function() {
|
||||||
var v;
|
var v;
|
||||||
@@ -18,7 +18,6 @@ describe('ol.Object', function() {
|
|||||||
it('returns undefined', function() {
|
it('returns undefined', function() {
|
||||||
expect(v).to.be(undefined);
|
expect(v).to.be(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('get a set property', function() {
|
describe('get a set property', function() {
|
||||||
@@ -32,6 +31,17 @@ describe('ol.Object', function() {
|
|||||||
expect(v).to.eql(1);
|
expect(v).to.eql(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('unset a set property', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
o.set('k', 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns undefined', function() {
|
||||||
|
var v = o.unset('k');
|
||||||
|
expect(v).to.be(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#get()', function() {
|
describe('#get()', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user