Creating an object that stores properties only when needed in BaseObject class
This commit is contained in:
@@ -128,7 +128,9 @@ class Feature extends BaseObject {
|
||||
* @api
|
||||
*/
|
||||
clone() {
|
||||
const clone = new Feature(this.getProperties());
|
||||
const clone = new Feature(
|
||||
this.hasProperties() ? this.getProperties() : undefined
|
||||
);
|
||||
clone.setGeometryName(this.getGeometryName());
|
||||
const geometry = this.getGeometry();
|
||||
if (geometry) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import Event from './events/Event.js';
|
||||
import ObjectEventType from './ObjectEventType.js';
|
||||
import Observable from './Observable.js';
|
||||
import {assign} from './obj.js';
|
||||
import {assign, isEmpty} from './obj.js';
|
||||
import {getUid} from './util.js';
|
||||
|
||||
/**
|
||||
@@ -94,10 +94,10 @@ class BaseObject extends Observable {
|
||||
getUid(this);
|
||||
|
||||
/**
|
||||
* @name values_
|
||||
* @private
|
||||
* @type {!Object<string, *>}
|
||||
*/
|
||||
this.values_ = {};
|
||||
|
||||
if (opt_values !== undefined) {
|
||||
this.setProperties(opt_values);
|
||||
@@ -112,7 +112,7 @@ class BaseObject extends Observable {
|
||||
*/
|
||||
get(key) {
|
||||
let value;
|
||||
if (this.values_.hasOwnProperty(key)) {
|
||||
if (this.values_ && this.values_.hasOwnProperty(key)) {
|
||||
value = this.values_[key];
|
||||
}
|
||||
return value;
|
||||
@@ -124,7 +124,7 @@ class BaseObject extends Observable {
|
||||
* @api
|
||||
*/
|
||||
getKeys() {
|
||||
return Object.keys(this.values_);
|
||||
return (this.values_ && Object.keys(this.values_)) || [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,7 +133,14 @@ class BaseObject extends Observable {
|
||||
* @api
|
||||
*/
|
||||
getProperties() {
|
||||
return assign({}, this.values_);
|
||||
return (this.values_ && assign({}, this.values_)) || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean} The object has properties.
|
||||
*/
|
||||
hasProperties() {
|
||||
return !!this.values_;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,11 +163,12 @@ class BaseObject extends Observable {
|
||||
* @api
|
||||
*/
|
||||
set(key, value, opt_silent) {
|
||||
const values = this.values_ || (this.values_ = {});
|
||||
if (opt_silent) {
|
||||
this.values_[key] = value;
|
||||
values[key] = value;
|
||||
} else {
|
||||
const oldValue = this.values_[key];
|
||||
this.values_[key] = value;
|
||||
const oldValue = values[key];
|
||||
values[key] = value;
|
||||
if (oldValue !== value) {
|
||||
this.notify(key, oldValue);
|
||||
}
|
||||
@@ -187,9 +195,12 @@ class BaseObject extends Observable {
|
||||
* @api
|
||||
*/
|
||||
unset(key, opt_silent) {
|
||||
if (key in this.values_) {
|
||||
if (this.values_ && key in this.values_) {
|
||||
const oldValue = this.values_[key];
|
||||
delete this.values_[key];
|
||||
if (isEmpty(this.values_)) {
|
||||
delete this.values_;
|
||||
}
|
||||
if (!opt_silent) {
|
||||
this.notify(key, oldValue);
|
||||
}
|
||||
|
||||
@@ -199,6 +199,11 @@ class EsriJSON extends JSONFeature {
|
||||
writeFeatureObject(feature, opt_options) {
|
||||
opt_options = this.adaptOptions(opt_options);
|
||||
const object = {};
|
||||
if (!feature.hasProperties()) {
|
||||
object['attributes'] = {};
|
||||
return object;
|
||||
}
|
||||
const properties = feature.getProperties();
|
||||
const geometry = feature.getGeometry();
|
||||
if (geometry) {
|
||||
object['geometry'] = writeGeometry(geometry, opt_options);
|
||||
@@ -214,9 +219,8 @@ class EsriJSON extends JSONFeature {
|
||||
),
|
||||
});
|
||||
}
|
||||
delete properties[feature.getGeometryName()];
|
||||
}
|
||||
const properties = feature.getProperties();
|
||||
delete properties[feature.getGeometryName()];
|
||||
if (!isEmpty(properties)) {
|
||||
object['attributes'] = properties;
|
||||
} else {
|
||||
|
||||
@@ -212,29 +212,32 @@ class GML2 extends GMLBase {
|
||||
context.serializers = {};
|
||||
context.serializers[featureNS] = {};
|
||||
}
|
||||
const properties = feature.getProperties();
|
||||
const keys = [];
|
||||
const values = [];
|
||||
for (const key in properties) {
|
||||
const value = properties[key];
|
||||
if (value !== null) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
if (
|
||||
key == geometryName ||
|
||||
typeof (/** @type {?} */ (value).getSimplifiedGeometry) === 'function'
|
||||
) {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
this.writeGeometryElement,
|
||||
this
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
writeStringTextNode
|
||||
);
|
||||
if (feature.hasProperties()) {
|
||||
const properties = feature.getProperties();
|
||||
for (const key in properties) {
|
||||
const value = properties[key];
|
||||
if (value !== null) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
if (
|
||||
key == geometryName ||
|
||||
typeof (/** @type {?} */ (value).getSimplifiedGeometry) ===
|
||||
'function'
|
||||
) {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
this.writeGeometryElement,
|
||||
this
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
writeStringTextNode
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -843,29 +843,32 @@ class GML3 extends GMLBase {
|
||||
context.serializers = {};
|
||||
context.serializers[featureNS] = {};
|
||||
}
|
||||
const properties = feature.getProperties();
|
||||
const keys = [];
|
||||
const values = [];
|
||||
for (const key in properties) {
|
||||
const value = properties[key];
|
||||
if (value !== null) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
if (
|
||||
key == geometryName ||
|
||||
typeof (/** @type {?} */ (value).getSimplifiedGeometry) === 'function'
|
||||
) {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
this.writeGeometryElement,
|
||||
this
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
writeStringTextNode
|
||||
);
|
||||
if (feature.hasProperties()) {
|
||||
const properties = feature.getProperties();
|
||||
for (const key in properties) {
|
||||
const value = properties[key];
|
||||
if (value !== null) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
if (
|
||||
key == geometryName ||
|
||||
typeof (/** @type {?} */ (value).getSimplifiedGeometry) ===
|
||||
'function'
|
||||
) {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
this.writeGeometryElement,
|
||||
this
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!(key in context.serializers[featureNS])) {
|
||||
context.serializers[featureNS][key] = makeChildAppender(
|
||||
writeStringTextNode
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,16 +207,22 @@ class GeoJSON extends JSONFeature {
|
||||
object.id = id;
|
||||
}
|
||||
|
||||
const geometry = feature.getGeometry();
|
||||
if (geometry) {
|
||||
object.geometry = writeGeometry(geometry, opt_options);
|
||||
if (!feature.hasProperties()) {
|
||||
return object;
|
||||
}
|
||||
|
||||
const properties = feature.getProperties();
|
||||
delete properties[feature.getGeometryName()];
|
||||
const geometry = feature.getGeometry();
|
||||
if (geometry) {
|
||||
object.geometry = writeGeometry(geometry, opt_options);
|
||||
|
||||
delete properties[feature.getGeometryName()];
|
||||
}
|
||||
|
||||
if (!isEmpty(properties)) {
|
||||
object.properties = properties;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user