Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 80c8e1e

Browse files
author
Scott J. Miles
committed
Simplify requireProperties logic, document the conventions
1 parent 2d3420d commit 80c8e1e

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/declaration/properties.js

+39-28
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,53 @@
7575
prototype._publishLC = this.lowerCaseMap(publish);
7676
}
7777
},
78-
// sync prototype to property descriptors;
79-
// desriptor format contains default value and optionally a
80-
// hint for reflecting the property to an attribute.
81-
// e.g. {foo: 5, bar: {value: true, reflect: true}}
82-
// reflect: {foo: true} is also supported
8378
//
84-
requireProperties: function(propertyDescriptors, prototype, base) {
85-
// reflected properties
79+
// `name: value` entries in the `publish` object may need to generate
80+
// matching properties on the prototype.
81+
//
82+
// Values that are objects may have a `reflect` property, which
83+
// signals that the value describes property control metadata.
84+
// In metadata objects, the prototype default value (if any)
85+
// is encoded in the `value` property.
86+
//
87+
// publish: {
88+
// foo: 5,
89+
// bar: {value: true, reflect: true},
90+
// zot: {}
91+
// }
92+
//
93+
// `reflect` metadata property controls whether changes to the property
94+
// are reflected back to the attribute (default false).
95+
//
96+
// A value is stored on the prototype unless it's === `undefined`,
97+
// in which case the base chain is checked for a value.
98+
// If the basal value is also undefined, `null` is stored on the prototype.
99+
//
100+
// The reflection data is stored on another prototype object, `reflect`
101+
// which also can be specified directly.
102+
//
103+
// reflect: {
104+
// foo: true
105+
// }
106+
//
107+
requireProperties: function(propertyInfos, prototype, base) {
108+
// per-prototype storage for reflected properties
86109
prototype.reflect = prototype.reflect || {};
87110
// ensure a prototype value for each property
88111
// and update the property's reflect to attribute status
89-
for (var n in propertyDescriptors) {
90-
var propertyDescriptor = propertyDescriptors[n];
91-
var reflects = this.reflectHintForDescriptor(propertyDescriptor);
92-
if (prototype.reflect[n] === undefined && reflects !== undefined) {
93-
prototype.reflect[n] = reflects;
112+
for (var n in propertyInfos) {
113+
var value = propertyInfos[n];
114+
// value has metadata if it has a `reflect` property
115+
if (value && value.reflect !== undefined) {
116+
prototype.reflect[n] = Boolean(value.reflect);
117+
value = value.value;
94118
}
95-
if (prototype[n] === undefined) {
96-
prototype[n] = this.valueForDescriptor(propertyDescriptor);
119+
// only set a value if one is specified
120+
if (value !== undefined) {
121+
prototype[n] = value;
97122
}
98123
}
99124
},
100-
valueForDescriptor: function(propertyDescriptor) {
101-
var value = typeof propertyDescriptor === 'object' &&
102-
propertyDescriptor ? propertyDescriptor.value : propertyDescriptor;
103-
return value !== undefined ? value : null;
104-
},
105-
// returns the value of the descriptor's 'reflect' property or undefined
106-
reflectHintForDescriptor: function(propertyDescriptor) {
107-
if (typeof propertyDescriptor === 'object' &&
108-
propertyDescriptor && propertyDescriptor.reflect !== undefined) {
109-
return propertyDescriptor.reflect;
110-
}
111-
},
112125
lowerCaseMap: function(properties) {
113126
var map = {};
114127
for (var n in properties) {
@@ -154,14 +167,12 @@
154167
this.createPropertyAccessor(n);
155168
}
156169
}
157-
158170
var n$ = prototype._computedNames;
159171
if (n$ && n$.length) {
160172
for (var i=0, l=n$.length, n, fn; (i<l) && (n=n$[i]); i++) {
161173
this.createPropertyAccessor(n);
162174
}
163175
}
164-
165176
}
166177
};
167178

0 commit comments

Comments
 (0)