Allow ol.Object property update without notification

Add an optional notify argument to object set and setProperties methods.

If the argument is not provided, set and setProperties trigger an event.

If you update many objects or do so frequently and do not need notification, performance is improved by setting silent to true.
This commit is contained in:
DavidHequet
2015-10-09 15:55:02 +02:00
committed by Tim Schaub
parent df460d2ed1
commit 069a81db74
+17 -8
View File
@@ -197,12 +197,17 @@ ol.Object.prototype.notify = function(key, oldValue) {
* Sets a value. * Sets a value.
* @param {string} key Key name. * @param {string} key Key name.
* @param {*} value Value. * @param {*} value Value.
* @param {boolean=} opt_silent Update property without triggering notification.
* @api stable * @api stable
*/ */
ol.Object.prototype.set = function(key, value) { ol.Object.prototype.set = function(key, value, opt_silent) {
var oldValue = this.values_[key]; if (opt_silent === true) {
this.values_[key] = value; this.values_[key] = value;
this.notify(key, oldValue); } else {
var oldValue = this.values_[key];
this.values_[key] = value;
this.notify(key, oldValue);
}
}; };
@@ -210,12 +215,13 @@ ol.Object.prototype.set = function(key, value) {
* Sets a collection of key-value pairs. Note that this changes any existing * Sets a collection of key-value pairs. Note that this changes any existing
* properties and adds new ones (it does not remove any existing properties). * properties and adds new ones (it does not remove any existing properties).
* @param {Object.<string, *>} values Values. * @param {Object.<string, *>} values Values.
* @param {boolean=} opt_silent update propertie silently
* @api stable * @api stable
*/ */
ol.Object.prototype.setProperties = function(values) { ol.Object.prototype.setProperties = function(values, opt_silent) {
var key; var key;
for (key in values) { for (key in values) {
this.set(key, values[key]); this.set(key, values[key], opt_silent);
} }
}; };
@@ -223,12 +229,15 @@ ol.Object.prototype.setProperties = function(values) {
/** /**
* Unsets a property. * Unsets a property.
* @param {string} key Key name. * @param {string} key Key name.
* @param {boolean=} opt_silent update propertie silently
* @api stable * @api stable
*/ */
ol.Object.prototype.unset = function(key) { ol.Object.prototype.unset = function(key, opt_silent) {
if (key in this.values_) { if (key in this.values_) {
var oldValue = this.values_[key]; var oldValue = this.values_[key];
delete this.values_[key]; delete this.values_[key];
this.notify(key, oldValue); if (!opt_silent) {
this.notify(key, oldValue);
}
} }
}; };