Skip to content

Bugfix: prop change not updating vue instance for async component #250

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/utils/createVueInstance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPropsData, reactiveProps } from './props';
import { getPropsData, reactiveProps, syncProps } from './props';
import { getSlots } from './slots';
import { customEmit } from './customEvent';

Expand Down Expand Up @@ -103,6 +103,7 @@ export default function createVueInstance(element, Vue, componentDefinition, pro
}

reactiveProps(element, props);
syncProps(element, props, options);

if (typeof options.beforeCreateVueInstance === 'function') {
rootElement = options.beforeCreateVueInstance(rootElement) || rootElement;
Expand Down
29 changes: 29 additions & 0 deletions src/utils/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ export function reactiveProps(element, props) {
});
}

/**
* When attribute changes we should update Vue instance
* @param element
* @param props
* @param options
*/
export function syncProps(element, props, options) {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
const attributeName = mutation.attributeName;
const propIndex = props.hyphenate.indexOf(attributeName);
if (mutation.type === "attributes" && propIndex !== -1) {
const oldValue = mutation.oldValue;
const value = element.hasAttribute(attributeName) ? element.getAttribute(attributeName) : undefined;
if (element.__vue_custom_element__ && typeof value !== 'undefined') {
const propNameCamelCase = props.camelCase[propIndex];
typeof options.attributeChangedCallback === 'function' && options.attributeChangedCallback.call(element, propNameCamelCase, oldValue, value);
const type = props.types[propNameCamelCase];
element.__vue_custom_element__[propNameCamelCase] = convertAttributeValue(value, type);
}
}
});
});
observer.observe(element, {
attributes: true,
attributeOldValue: true
})
}

/**
* In root Vue instance we should initialize props as 'propsData'.
* @param instanceOptions
Expand Down
20 changes: 1 addition & 19 deletions src/vue-custom-element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import registerCustomElement from './utils/registerCustomElement';
import createVueInstance from './utils/createVueInstance';
import { getProps, convertAttributeValue } from './utils/props';
import { camelize } from './utils/helpers';
import { getProps } from './utils/props';

function install(Vue) {
Vue.customElement = function vueCustomElement(tag, componentDefinition, options = {}) {
Expand Down Expand Up @@ -60,23 +59,6 @@ function install(Vue) {
}, options.destroyTimeout || 3000);
},

/**
* When attribute changes we should update Vue instance
* @param name
* @param oldVal
* @param value
*/
attributeChangedCallback(name, oldValue, value) {
if (this.__vue_custom_element__ && typeof value !== 'undefined') {
const nameCamelCase = camelize(name);
typeof options.attributeChangedCallback === 'function' && options.attributeChangedCallback.call(this, name, oldValue, value);
const type = this.__vue_custom_element_props__.types[nameCamelCase];
this.__vue_custom_element__[nameCamelCase] = convertAttributeValue(value, type);
}
},

observedAttributes: props.hyphenate,

shadow: !!options.shadow && !!HTMLElement.prototype.attachShadow
});

Expand Down