-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix registerPrimitive property merging (fixes #1324) #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ var registerElement = require('../../core/a-register-element').registerElement; | |
| var utils = require('../../utils/'); | ||
|
|
||
| var debug = utils.debug; | ||
| var log = debug('extras:primitives'); | ||
| var log = debug('extras:primitives:debug'); | ||
|
|
||
| module.exports = function registerPrimitive (name, definition) { | ||
| name = name.toLowerCase(); | ||
|
|
@@ -46,16 +46,10 @@ module.exports = function registerPrimitive (name, definition) { | |
| var self = this; | ||
| var attributes = this.attributes; | ||
|
|
||
| // Apply default components. | ||
| this.componentData = cloneObject(this.defaultAttributes); | ||
| Object.keys(this.componentData).forEach(function (componentName) { | ||
| if (!self.hasAttribute(componentName)) { | ||
| self.setAttribute(componentName, self.componentData[componentName]); | ||
| } | ||
| }); | ||
| this.applyDefaultComponents(); | ||
|
|
||
| // Apply initial attributes. | ||
| Object.keys(attributes).forEach(function (attributeName) { | ||
| Object.keys(attributes).forEach(function applyInitial (attributeName) { | ||
| var attr = attributes[attributeName]; | ||
| self.syncAttributeToComponent(attr.name, attr.value); | ||
| }); | ||
|
|
@@ -75,6 +69,33 @@ module.exports = function registerPrimitive (name, definition) { | |
| } | ||
| }, | ||
|
|
||
| applyDefaultComponents: { | ||
| value: function () { | ||
| var self = this; | ||
| var defaultData = this.defaultAttributes; | ||
|
|
||
| // Apply default components. | ||
| Object.keys(defaultData).forEach(function applyDefault (componentName) { | ||
| var componentData = defaultData[componentName]; | ||
|
|
||
| // Set component properties individually to not overwrite user-defined components. | ||
| if (componentData instanceof Object && Object.keys(componentData).length) { | ||
| Object.keys(componentData).forEach(function setProperty (propName) { | ||
| // Check if component property already defined. | ||
| var definedData = self.getAttribute(componentName); | ||
| if (definedData && definedData[propName] !== undefined) { return; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or do we want to just check
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but couldn't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's valid. But we're checking for |
||
|
|
||
| self.setAttribute(componentName, propName, componentData[propName]); | ||
| }); | ||
| return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we should
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only returning here to mimic an
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, I guess I understand |
||
| } | ||
|
|
||
| // Component is single-property schema, just set the attribute. | ||
| self.setAttribute(componentName, componentData); | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| /** | ||
| * If attribute is mapped to a component property, set the component property using | ||
| * the attribute value. | ||
|
|
@@ -104,20 +125,14 @@ module.exports = function registerPrimitive (name, definition) { | |
| // Run transform. | ||
| value = this.getTransformedValue(attr, value); | ||
|
|
||
| // Initialize internal component data if necessary. | ||
| if (!this.componentData[componentName]) { | ||
| this.componentData[componentName] = this.defaultAttributes[componentName] || {}; | ||
| } | ||
|
|
||
| // Update internal component data. | ||
| // If multi-property schema, set as update to component to not overwrite. | ||
| if (propertyName) { | ||
| this.componentData[componentName][propertyName] = value; | ||
| } else { | ||
| this.componentData[componentName] = value; | ||
| this.setAttribute(componentName, propertyName, value); | ||
| return; | ||
| } | ||
|
|
||
| // Put component data. | ||
| this.setAttribute(componentName, this.componentData[componentName]); | ||
| // Single-property schema, just set the value. | ||
| this.setAttribute(componentName, value); | ||
| } | ||
| }, | ||
|
|
||
|
|
@@ -133,21 +148,3 @@ module.exports = function registerPrimitive (name, definition) { | |
| }) | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Clone an object, including inner objects one-level deep. | ||
| * Used for copying defaultAttributes to componentData so primitives of the same type don't | ||
| * affect each others' defaultAttributes object. | ||
| */ | ||
| function cloneObject (obj) { | ||
| var clone = {}; | ||
| Object.keys(obj).forEach(function (key) { | ||
| var value = obj[key]; | ||
| if (typeof value === 'object') { | ||
| clone[key] = utils.extend({}, value); | ||
| } else { | ||
| clone[key] = value; | ||
| } | ||
| }); | ||
| return clone; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like naming the anonymous functions 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add it to our linter #1033
But I don't want that to catch in our test files where there are hundreds of anon functions.