Perform the hook 'beforeUpdate' only once in Vuejs

Asked

Viewed 54 times

0

I got a code pad inside the hook beforeUpdate() of the Vue instance, and it is running several times in a row.

My question is:

It’s normal to hook beforeUpdate() be called several times?? If not, how do I avoid this loop that it keeps running??

beforeUpdate () {
  this.pageTimeGround = this.initTimeGroud(this.timeGround)
  let maxTime = this.pageTimeGround[this.pageTimeGround.length - 1]
  let minTime = this.pageTimeGround[0]
  let maxMin = maxTime.split(':')[0] * 60 + maxTime.split(':')[1] * 1
  let minMin = minTime.split(':')[0] * 60 + minTime.split(':')[1] * 1
  for (let i = 0; i < this.taskDetail.length; i++) {
    for (let j = 0; j < this.taskDetail[i].length; j++) {
      let startMin = this.taskDetail[i][j].dateStart.split(':')[0] * 60 + this.taskDetail[i][j].dateStart.split(':')[1] * 1
      let endMin = this.taskDetail[i][j].dateEnd.split(':')[0] * 60 + this.taskDetail[i][j].dateEnd.split(':')[1] * 1
      if (startMin < minMin || endMin > maxMin) {
        this.taskDetail[i].splice(j, 1)
        j--
        continue
      }
      let difMin = endMin - startMin
      this.taskDetail[i][j].styleObj = {
        height: difMin * 50 / 60 + 'px',
        top: ((startMin - (this.pageTimeGround[0].split(':')[0] * 60 + this.pageTimeGround[0].split(':')[1] * 1)) * 50 / 60) + 25 + 'px',
        backgroundColor: this.color[~~(Math.random() * this.color.length)]
      }
    }
  }
}

This code is lib Vue-Schedule, and is within and currently within the created(), but I need that whenever there is a change in data() of the DOM instance be re-rendered as I am working with an event schedule and notifications through Socket.io

  • add your code to analyze

  • Added the code @Felipeduarte

  • 2

    Good only with the section of the function I believe that it will not be possible to identify the problem, because the structure of operation of beforeUpdate is wide. Basically it is requested before the date change, but it listens to all changes, that is, if in my scope I call a function that changes 3 variables on the date, beforeUpdate will be executed 3 times, it may be your case.

  • That’s exactly what happens, I’m trying to find a solution and I’m not getting...

  • you predict to actually use beforeUpdate ? it is very generic, you need to run it when? it is only after a function or several? if you can add the function that changes the date it would be interesting

  • 1

    I managed to solve the problem, what happened was the following: He was rendering the component before he even completed the assembly and the calculations of height and width to apply the style, and to test this I put a v-if with a boolean with false value and with a setTimeout() only 100ms delay in rendering it worked right. Now I just need to make the code in a way that the delay time is not fixed. Thank you so much for your attention and availability to help @Felipeduarte...

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.