Ways to update a component when the value of a Promise is altered

I am struggling with Vue component re-rendering due to a problem related to consuming data from a Promise. The data is fetched and stored under the specific property chain (visualData.layout.cube...), where I assign values to DATA properties (such as label, indicatorValue, etc).

The issue is that the component does not re-render when the data within the Promise changes (e.g. when applying application filters or interacting with charts). The component's data only updates when switching routes or reloading the page.

What Vue solution can be used to monitor changes coming from the Promise?

data() {
    return {
        label: null,
        indicatorState: null,
        indicatorValue: null,
    }
},
computed: {
  isAppReady() {
    return this.$store.getters.isAppReady;
  },
},
mounted() {
  this.$store.watch(() => this.isAppReady, (status) => {
    if (status) { // if STATUS === true
      this.getData();
    }
  });

  if (this.isAppReady) {
    this.getData();
  }
},
methods: {
  getData() {
    return this.appMethods // global object gathering specific methods like .getObject, .getField etc.
      .getObject(null, this.visualID)
      .then(visualData => visualData.layout.cube.dataPages[0].matrix[0])
      .then((transformedData) => {
        this.label = transformedData[0].qText;
        this.indicatorValue = transformedData[1].qText;
        this.indicatorState = Number(transformedData[1].qNum) > 0;
      });
  },
}, 

Answer №1

When utilizing Vuex, it is recommended to separate data fetching logic from the component,

  • Transfer the getData() method to a service
  • Invoke it in the click handler of any component where a refresh is needed
  • Add new data to the store
  • Retrieve the properties from the store

Service code

getData() {
  return this.appMethods 
    .getObject(null, this.visualID)
    .then(visualData => visualData.layout.cube.dataPages[0].matrix[0])
    .then((transformedData) => {
      this.$store.commit('SET_MY_DATA', transformedData);
    });
}

Access via computed properties in the component

computed: {
  label() {
    return this.$store.getters.label
  } 
  indicatorState() {
    return this.$store.getters.indicatorState
  } 
  indicatorValue() {
    return this.$store.getters.indicatorValue
  } 
},

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

The text is not appearing properly in an HTML file due to issues with

Hi trying to display the text received from the controller's scope. Here's what I have attempted: HTML: <div ng-repeat="item in sResults"></div> Controller JavaScript: $scope.sResults = []; function App( ){ var Label ...

Is there a way for a Vue component to interact with a button or checkbox in order to dynamically update tooltip content and button style using a function?

Is it possible for a Vue component to trigger button clicks or checkbox checks in order to update tooltip text and button color using a function? Despite the presence of a handle() function in the code provided, these changes are not currently taking effec ...

The constructor error in ng-serve signalizes an issue in

Currently, I am developing an AngularJS application. However, when attempting to start the dev server, I encountered an issue with my ng serve command: https://i.stack.imgur.com/QujSe.png ...

Adjust the ARIA value of a Bootstrap progress bar within a VueJS component on the fly

During the development of my website using VueJS and Bootstrap, I made the decision to utilize Vanilla Bootstrap instead of a specific VueJS framework like VueBootstrap or VueStrap. Currently, I am facing an issue while building a custom component that wr ...

The Highchart column chart is triggering the drilldown event multiple times

I have created a column chart using Highchart and I am facing an issue with the drilldown functionality. Whenever I click on a bar multiple times, the drilldown event triggers multiple times as well. This results in the drilldown event being triggered repe ...

An error occurred while attempting to retrieve data from a JSONArray

I have been working on creating a phonegap plugin for Android where I am returning a JSONArray using callBackContext.sendPluginResult(result);. Below is the code snippet demonstrating how I am constructing the JSONArray: private JSONArray makeJsonObject(S ...

Is there a way to transform a JavaScript array into JSON format in order to use it as the returned data from a RESTful API

Currently, I am exploring how to efficiently convert an array of arrays into a JSON string for retrieval through a RESTful API. On my server, data is fetched from a database in the format: {"user":"some name","age":number ...

Transferring an array from PHP to jQuery through the use of AJAX

My JavaScript code communicates with a PHP page to retrieve data from a database and store it in an array. Now, I would like to use jQuery to loop through that array. This is how the array is structured: Array ( [0] => Array ( [image] => articl ...

Can Express.Multer.File be inserted into a FormData object easily?

Can anyone assist me in figuring out how to add a file of type Express.Multer.File into a FormData object? Situation: I have received a file with the type Express.Multer.File (I am using nestjs and followed this part of the documentation: https://docs.nes ...

Using javascript within a PHP file

I'm struggling to implement this JavaScript function within a PHP page (footer.php), but it's not functioning as expected. I've experimented with various approaches, even attempting to include the script within a PHP echo statement, but that ...

Change the name of a file to a name chosen by the user while uploading it to an

I'm a beginner when it comes to node.js and I'm facing an issue that I couldn't find a solution for despite looking into various related topics. My problem involves using Node.js on the server side to upload a file and rename it based on a ...

Encountered a Node.js build error related to a Java module

Encountering issues while attempting to integrate the JAVA module into my NodeJS application on Ubuntu: NodeJS: v6.2.0 NPM: 3.8.9 Node-gyp: v3.3.1 Python: 2.7.12 GCC: 5.4.0 Despite having all the required dependencies, I consistently face errors when ...

Why is "undefined" being used to alert an ajax call response?

I am encountering an issue with a returned value from an AJAX request. The web method being referenced returns a boolean value of true or false. However, when attempting to access this value outside the AJAX method, I am receiving an "undefined" message :? ...

Is there a way to increase the total of each row by two when a button is clicked?

Looking to enhance the sum of each row by two depending on whether a button is clicked. HTML: <table> <tr> <td> <input type="checkbox" class="prof" name="prof" value="0"> <input class=&quo ...

Receiving undefined values when passing down props from an asynchronous API call

After diving into single file components in Vue in an effort to increase modularity, I've encountered issues with passing down props when they are set by an async response from an API. Currently, my project involves: parent component child comp ...

Intercepting and handling errors thrown by a Node module

I have been encountering an issue with a module that throws errors without them being returned as part of the callback, causing the program to stop when an error is thrown. The code snippet I am using looks like this: co(function*(){ try{ for (let ...

Utilize Chrome storage instead of localstorage to generate Parse sessions

I'm currently developing a Chrome Extension that relies on Parse User sessions. Because localstorage is limited to specific domains, I am looking to utilize chrome.storage so the data can be accessed across any site. The existing Parse Javascript SDK ...

Ways to style a div element in CSS to achieve a unique shape

Hello there! I'm looking to achieve a tilted background div effect. Anyone have any tips or ideas on how I can do this? I'm new to web development and would appreciate the guidance. https://i.stack.imgur.com/wyj1X.png ...

Issue: The component series.line does not exist. Please ensure it is loaded before using it in Vue Echarts

I added the line component, but I'm still encountering issues. I set up my project using vue cli 3 and referred to this guide, but I can't locate the vue.config.js file in my project. Therefore, I manually created a vue.config.js and placed it in ...

Ordering an array based on two integer properties using JavaScript

Here is an array of objects I am working with: const items = [ { name: "Different Item", amount: 100, matches: 2 }, { name: "Different Item", amount: 100, matches: 2 }, { name: "An Item", amount: 100, matches: 1 }, { name: "Different Item" ...