Dataflow in the application¶
The frontend GUI, unsurprisingly, presents data from the backend to the user. Data is fetched from the backend via REST API and fed to its various parts via a mix of component properties and application state (ie. Vuex).
This chapter describes these dataflows in detail.
Dataflow when fetching and displaying information¶
Note that the below example describes an ideal dataflow and that the codebase might differ from it in its current state.
The frontend application goes through the following staps to display data to the user:
User navigates to a page and its corresponding main component picks up the route parameters
Main components use route params to fetch data via REST API
Subcomponents are updated via props or state
Data is subsequently displayed to the user via Vue template bindings
Let’s explore these steps in greater detail …
Compontent pick up route parameters¶
The route’s main component picks up the route parameters (ex. route.params.actId) at the created lifecycle hook and at route update events like so:
created: function() {
this.activity_id = this.$route.params.actId
},
beforeRouteUpdate(to, from, next) {
this.activity_id = to.params.actId
next()
}
main component uses route params to fetch data¶
The main component uses route params to fetch data via REST API.
Using state:
this.$store.dispatch('fetchActivity', this.activity_id)
and a corresponding getter to get the fetched information:
computed: {
activity_data: function() {
return this.$state.getters.getActivity()
}
}
Not using state: Fetching data directly using axios if no state update is needed:
axios.get(`/activity/${ this.activity_id }/`).then(response => {
this.activity_data = response.data
})
Subcomponents are updated via props or state¶
Subcomponents that are included in the main component may be passed the fetched data as a prop or get data from state.
Using state: If we can assume the main component has updated the state, fetch data from state using a getter:
computed: {
activity_data: function() {
return this.$state.getters.getActivity()
}
}
Not using state:
The component listens for changes to props in order to update its content:
props: [
'activityData'
],
data: function() {
return {
activity_data: this.activityData
}
},
watch: {
activityData: function(new_value, old_value) {
this.activity_data = new_value
}
}
Dataflow when manipulating data¶
Here is another idealized dataflow for when users create or update data via the UI:
User changes a value in an input element
A
POSTorPATCHrequest is sent to REST APIREST API response updates state or current template
Let us explore these steps in greater detail …
User changes a value in an input element¶
Typically, a user will interact with an <input> element of some sort that is bound to a component’s variables via v-model or via a method run on a change or input event.
A POST or PATCH request is sent to REST API¶
User input can cause a method call to send a POST/PATCH request in different ways.
By event
<template>
<input type="text" @input="inputUpdateHandler">
</template>
<script>
...
methods: {
inputUpdateHandler: function(ev) {
axios.post('/endpoint/', ev.target.value)
}
}
...
</script>
By value binding
<template>
<input type="text" v-model="input_value">
</template>
<script>
...
watch: {
input_value: function(new_value, old_value) {
axios.post('/endpoint/', new_value)
}
}
...
</script>
REST API response updates state or current template¶
When a REST API request is successful, it should update the state by mutation call (commit).
If no state applies, it should update the current component’s properties.