3

I want to check if an object has new subobjects. I mean I have an object with x nested objects, but I will check it if the main object has new objects. Is there any existing solution to check this out?

methods: {
    playSound(newOrders, sound) {
      if (newOrders.length > 0) {
        if (sound) {
          var audio = new Audio(sound);
          audio.play();
        }
      }
    }

  },
  beforeUpdate() {
    this.playSound(this.newOrders, 'http://soundbible.com/mp3/Elevator Ding-SoundBible.com-685385892.mp3')
  },

My goal is to call the function playSound if the newOrders object has new items. This code calls the function every time when the array changes. Also when some items are removed. This part of the functionality is wrong. Any idea to fix it?

Rick
  • 4,030
  • 9
  • 24
  • 35
robertkovacs
  • 435
  • 3
  • 14

2 Answers2

1

You should use a computed property and a watcher, and you will get he results you are expecting.. Watch changes from "newOrders"

You should use Vue Watchers. Here you will find an example (and answer) for your question.

Vue.js - How to properly watch for nested data

tony19
  • 125,647
  • 18
  • 229
  • 307
mauriblint
  • 1,802
  • 2
  • 29
  • 46
0

use watch property, which will listen to every changes in object. you can google search for vue watch, so you will get the updated code content.

Deepesh
  • 102
  • 7