add componentID

This commit is contained in:
Harvey Zack committed 2019-06-12 17:08:45 +08:00
1 parent 999636400e
commit 4503676cbf
1 file changed
+4 -3
+4 -3
View File
@@ -3,7 +3,8 @@ import { append, setStyles, setStyle, remove, errorHandle, hasOwnProperty } from
export default function component(art, parent, target, getOption, callback, title) { export default function component(art, parent, target, getOption, callback, title) {
const option = typeof getOption === 'function' ? getOption(art) : getOption; const option = typeof getOption === 'function' ? getOption(art) : getOption;
if (!option.disable) { if (!option.disable) {
const name = option.name || `${title}${parent.id}`; const componentID = parent.id;
const name = option.name || `${title}${componentID}`;
errorHandle( errorHandle(
!hasOwnProperty(parent, name), !hasOwnProperty(parent, name),
`Cannot create a component that already has the same name: ${title} -> ${name}`, `Cannot create a component that already has the same name: ${title} -> ${name}`,
@@ -20,7 +21,7 @@ export default function component(art, parent, target, getOption, callback, titl
} }
const childs = Array.from(target.children); const childs = Array.from(target.children);
$element.dataset.index = option.index || parent.id; $element.dataset.index = option.index || componentID;
const nextChild = childs.find(item => Number(item.dataset.index) >= Number($element.dataset.index)); const nextChild = childs.find(item => Number(item.dataset.index) >= Number($element.dataset.index));
if (nextChild) { if (nextChild) {
nextChild.insertAdjacentElement('beforebegin', $element); nextChild.insertAdjacentElement('beforebegin', $element);
@@ -47,7 +48,7 @@ export default function component(art, parent, target, getOption, callback, titl
Object.defineProperty(parent, name, { Object.defineProperty(parent, name, {
value: { value: {
get id() { get id() {
return parent.id; return componentID;
}, },
get $ref() { get $ref() {
return $element; return $element;