MCPcopy
hub / github.com/aframevr/aframe / setAttribute

Method setAttribute

src/core/a-entity.js:629–679  ·  view source on GitHub ↗

* setAttribute can: * * 1. Set a single property of a multi-property component. * 2. Set multiple properties of a multi-property component. * 3. Replace properties of a multi-property component. * 4. Set a value for a single-property component, mixin, or normal HTML attribute. *

(attrName, arg1, arg2)

Source from the content-addressed store, hash-verified

627 * it is a boolean indicating whether to clobber previous values (defaults to false).
628 */
629 setAttribute (attrName, arg1, arg2) {
630 var singlePropUpdate = AEntity.singlePropUpdate;
631
632 var newAttrValue;
633 var clobber;
634 var componentName;
635 var delimiterIndex;
636 var isDebugMode;
637 var key;
638
639 delimiterIndex = attrName.indexOf(MULTIPLE_COMPONENT_DELIMITER);
640 componentName = delimiterIndex > 0 ? attrName.substring(0, delimiterIndex) : attrName;
641
642 // Not a component. Normal set attribute.
643 if (!COMPONENTS[componentName]) {
644 if (attrName === 'mixin') { this.mixinUpdate(arg1); }
645 super.setAttribute.call(this, attrName, arg1);
646 return;
647 }
648
649 // Initialize component first if not yet initialized.
650 if (!this.components[attrName] && this.hasAttribute(attrName)) {
651 this.updateComponent(
652 attrName,
653 window.HTMLElement.prototype.getAttribute.call(this, attrName));
654 }
655
656 // Determine new attributes from the arguments
657 if (typeof arg2 !== 'undefined' &&
658 typeof arg1 === 'string' &&
659 arg1.length > 0 &&
660 typeof utils.styleParser.parse(arg1) === 'string') {
661 // Update a single property of a multi-property component
662 for (key in singlePropUpdate) { delete singlePropUpdate[key]; }
663 newAttrValue = singlePropUpdate;
664 newAttrValue[arg1] = arg2;
665 clobber = false;
666 } else {
667 // Update with a value, object, or CSS-style property string, with the possibility
668 // of clobbering previous values.
669 newAttrValue = arg1;
670 clobber = (arg2 === true);
671 }
672
673 // Update component
674 this.updateComponent(attrName, newAttrValue, clobber);
675
676 // In debug mode, write component data up to the DOM.
677 isDebugMode = this.sceneEl && this.sceneEl.getAttribute('debug');
678 if (isDebugMode) { this.components[attrName].flushToDOM(); }
679 }
680
681 /**
682 * Reflect component data in the DOM (as seen from the browser DOM Inspector).

Callers

nothing calls this directly

Calls 4

mixinUpdateMethod · 0.95
updateComponentMethod · 0.95
flushToDOMMethod · 0.80
getAttributeMethod · 0.45

Tested by

no test coverage detected