0

I have a watch property that watches some options passed to the data function.

Every time an option changes the watch handler gets triggered as I expect. Now, I would also like to fire the handler whenever I dynamically change the DOM structure.

At the moment I was using an ugly hack to force the handler to get fired. I was setting an option again with an &:

this.options.navigation = this.options.navigation & true;

Is there some way I can manually call the watch handler? Something like:

this.fireWatch();

My vue file:

  export default {
    props: {
      options: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },


    watch: {
      options: {
        deep: true,
        handler() {
          this.init();
        },
      },
    },
    mounted() {
      this.init();
    },

    methods: {
      init() {
        $(this.$el).myPlugin({
          ...this.options,
          ...this.events,
        });
      },
      emitEvent(name, args) {
        // Emit event on Vue way
        this.$emit.apply(this, [camelToKebab(name), ...args]);

        // Run event's handler with non Vue way
        if (this.options.hasOwnProperty(name)) {
          this.options[name].apply(this, args);
        }
      },
    }
 }
Rob
  • 14,746
  • 28
  • 47
  • 65
Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • 1
    Can't you manually call your init method? – Ferrybig Jan 24 '18 at 12:58
  • How can I call a component method from a vue instance? `this.$children[0].init()` ? Seems a bit ugly :( – Alvaro Jan 24 '18 at 13:21
  • [I found](https://stackoverflow.com/a/37521683/1081396) i can also call it using `ref="foo"`, then use `this.$refs.init()` but... still a bit ugly and relies on people using `ref="foo"` in their component. – Alvaro Jan 24 '18 at 13:34
  • Why does the parent need to call the element? If its the content of a slot thats updated, would the following work? https://stackoverflow.com/questions/38148297/in-vue-js-can-a-component-detect-when-the-slot-content-changes – Ferrybig Jan 24 '18 at 13:44
  • I would rather prefer to give total control to developers over when they want to update the component. Using emits or MutationObserver takes the control away from them and might not be desirable in some occasions. – Alvaro Jan 24 '18 at 17:08

0 Answers0