Vue automatically refreshes momentjs dates prior to making changes to the array

I am dealing with a situation where my child component receives data from its parent and, upon button click, sends an event to the parent via an event bus.

Upon receiving the event, I trigger a method that fetches data using a Swagger client.

The goal is to update an array of activities after each click on the child component.

Within the activities array, there are items with a date property which is converted to a formatted string using Moment.js: {{myFromNow(item.date)}}

The issue arises when I fetch a fresh list of activities (and set it to vm.activities), where the topmost item's date briefly refreshes before being replaced by the new item. This flickering of the new date is noticeable visually.

methods:{
    getActivity(){
    let vm = this

      this.$swagger.Activity.Activity_activityAll(
        {
          filter: JSON.stringify({
            order: 'Date DESC',
            include: [{
              relation: 'person',
              scope: {
                order: 'DateCreated ASC'
              }
            }
            ]
          })
        }
      )
        .then(function (resp) {


          vm.$set(vm, 'activity', resp.body['activity'])


        })
}
}

The listener for the event is set up in the mounted hook:

  mounted () {
    let vm = this
    this.$eventBus.$on('childAction', () => {

      vm.getActivity()

    })
  },

When debugging, it is evident that the fromNow function is updated after the

vm.$set(vm, 'activity', resp.body['activity'])
operation, and the list is only fully updated at the end of the method execution: https://i.stack.imgur.com/j9oOk.png

Do you have any insights into what might be causing this behavior?

Answer №1

After analyzing the issue, it was discovered that another parallel call was causing the re-rendering of all reactive data before the initial ajax call had completed.

The solution involved adjusting the data in this.activity within a computed property by including the fromNow date to activities and then iterating over that modified property.

Here's how it can be done:

computed: {
    compActivities () {
      this.activities.forEach((item, index, arr) => {
        arr[index].ActivityDateInterval = moment(item.ActivityDate).fromNow(true)
      })
      return this.activities
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a way to allow users to download html2canvas with a customized filename

I have a div where I want to use html2canvas to save a PNG file of its content. The issue is that the name of the saved file is static in my code and does not change unless I manually edit it. Is there a way to dynamically set the filename based on user i ...

The jQuery load() method may not load all elements

I've been struggling with a query issue for quite some time now. I have a Content Management System that I want to integrate into my website, but unfortunately, I am unable to use PHP includes. As an alternative, I decided to utilize jQuery instead. D ...

Personalized index.html for nuxt.js deployment

Apologies if this question has been asked before, but I'm unsure of what terms to search for. Is there a method for altering the template utilized to create the index.html file while constructing a Nuxt app in spa mode? ...

The HTML status code is 200, even though the JQuery ajax request shows a status code of 0

My issue is not related to cross site request problem, which is a common suggestion in search results for similar questions. When attempting to make an ajax request using jquery functions .get and .load, I'm receiving xhr.status 0 and xhr.statusText ...

Encountering an issue with my Discord bot where it displays the error message "Unable to access property 'cache' of an undefined object"

I encountered an issue while setting up discord-xp for my bot. Whenever I attempted to use the leaderboard command, an error message popped up: username: client.users.cache.get(key.userID) ? client.users.cache.get(key.userID).username : "Unknown" ...

Persistent Vue oAuth login amidst Express error issues

I have set up an Express server on localhost:5000 and a Vue app on localhost:8080 with discord-passport oAuth2 login. The issue arises when I log in and try to make a request through my Vue service handler to localhost:5000/api/users. In the browser, it di ...

Customizing AngularJS Scripts to Include Target Blank

I'm feeling a bit lost. I need to include a target="_blank" on my anchor link. The issue is that the anchor tag is linked to a script in angular. I am not familiar with this JavaScript framework. I have tried searching through the documentation for po ...

Using JavaScript to Retrieve, Manipulate, and Merge JSON Data from Various Files

My knowledge of JavaScript is limited, but I am interested in uploading multiple JSON files, processing them, converting them to text, combining them, and downloading them into a single JS file. I have successfully achieved this for a single file, but I a ...

Express js is unable to retrieve the page at /page

I'm facing a persistent routing error on express.js. Despite multiple attempts to fix it by referring to the documentation and making changes, I'm still stuck. Here's the basic content of the link pointing to the '/sell' route: i ...

Effective ways to check for the time discrepancy between dates in moment.js

My server is set to use UTC date format and I am running my node server in UTC as well. I am looking to determine if the current time in Indian timezone (which is +5.30) is greater than 8AM, and if so, send an email notification. How can I achieve this u ...

TypeError: The firestore function within the firebase_app__WEBPACK_IMPORTED_MODULE_0__ object is not recognized in Vue.js

I encountered a problem with my firebase.js file where I received the error message Uncaught TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0__.firestore is not a function in my console. Despite trying different methods to import firebase, none of them ...

The router link is unable to redirect to the desired page

neither the static router nor the dynamic router can navigate to the page. Here is the HTML code: <router-link to="/add_peer">Test</router-link> <router-link :to="{path:'/add_peer'}">Test</router-link> ...

I'm receiving a Reference Error stating that 'next()' is not defined, despite the fact that I have defined it in my JavaScript code. What could be causing this issue?

I have a javascript function called next() to process information and then call the function from HTML. However, my Firefox console is showing a ReferenceError: 'next' is not defined. I am aware that there is an error in my code, but I cannot pin ...

Converting JSON into HTML has me completely baffled

Despite my best efforts, I have managed to navigate my way through the code thus far, but I am puzzled as to why the JSON data is not being sent to the HTML via JavaScript. I can manually input the necessary parts in the developer console after onLoad an ...

Assign the array value to all inputs that share the same class tag

Does anyone know how to iterate through all tags with the same className and retrieve their values? var quantities = []; $(".add_more_items").each(function(){ quantities.push($(this).val()); }); Here is an example of the result: ['1&apo ...

Connecting to a pathway of Material-UI menu items

I am currently utilizing Material-UI's menu components as part of my project requirements. However, I am encountering difficulties in properly routing each MenuItem to an existing route within my application. As a temporary solution, I have resorted ...

Tips for maintaining consistent header and footer while developing the front end using JavaScript

Is it possible to maintain the same header and footer on all pages of a website if using JavaScript for the front end and PHP for the back end? While I am comfortable doing this with PHP, I am curious if it can also be achieved with JavaScript or HTML, o ...

The div element fails to display even after invoking a JavaScript function to make it visible

As a newcomer to JavaScript & jQuery, please bear with me as I ask a very basic question: I have created the following div that I want to display when a specific JavaScript function is called: <div id='faix' class="bg4 w460 h430 tac poa ...

Ways to transfer a value to another component in React

Currently, I am working on a project where users can add, edit, or delete movies from a list. While the addition and deletion functionalities are working fine, I am facing challenges with the editing feature. In my Movie component, I have included a text i ...

Adjusting the size of icons to fit within a container

I need a div that can display up to 7 icons, depending on certain selections. These icons are from ionicons library. Here is the current code snippet: <div class="item item-text-wrap" style="text-align:center;"> <button class="button" style=" ...