dangvanthanh / vue-ckeditor2

CKEditor 4 wrapper by Vue.js
https://vue-ckeditor2.surge.sh/
MIT License
171 stars 64 forks source link

[Bug] Infinite update loop when parent changes value prop, browser crash #34

Closed Lelectrolux closed 7 years ago

Lelectrolux commented 7 years ago

Code to reproduce bug :

// TestingBug.vue
<template>
    <div>
        <ck-editor v-model="text"></ck-editor>

        <button @click="text = '<p>Lorem ipsum</p>'">Send</button>
    </div>
</template>

<script type="text/babel">
    import CkEditor from 'vue-ckeditor2'

    export default {
        data() {
            return {
                text: '<p>dolor sit amet.</p>'
            }
        },
        components: {CkEditor}
    }
</script>

Once the page is loaded, add something in the ckeditor, then click the button.

From what I've understood, the beforeUpdate of the TestingBug component is called, which in turn calls the beforeUpdate of the CkEditor component, etc.


Fix :

Use a watcher instead of a beforeUpdate hook. Replace that hook with :

    export default {
        // ...

        watch: {
            value(value) {
                const { id } = this;
                if (value !== CKEDITOR.instances[id].getData()) {
                    CKEDITOR.instances[id].setData(value)
                }
            }
        },

        // ...
    }
dangvanthanh commented 7 years ago

@Lelectrolux Thank you for contribution.