33

Imagine this simple case. You have a Vue JS application in which users can create lists of tasks and sort them. These lists should be stored in a database by the server. Let's assume we have a ListComponent which does the bulk of the UX.

My question is, which pattern should I use to handle front-end and back-end data synchronisation?

A) Go through vuex: Store the active lists (those being shown, edited or created) in the vuex store. The ListComponent will change the store and then, the changes made to a list will be sent to the backend through an API.

B) Go directly to the server: Read and write directly from the ListComponent to the server every time a list is shown, edited or created.

If following A, what architecture should the store have? How and when should I kick a synchronisation? How can I keep track of what has changed and what has not?

1
  • 1
    I have just spent a few hours trying to go with A. I ended up with an empty component and all the logic in the store module. Seemed like a lot of overhead, so I am going back to having logic and API calls within my component. Apr 23, 2017 at 19:16

1 Answer 1

29

Both can be correct depending on your use case. The application I'm currently building uses both.

When to use B: Go Directly to the server Use B if the data is very important to save. In this case you may want to go directly to the server and serve up the response to verify it was committed to the DB. We use this process for Admin type changes. Note that you can also update Vuex after the server response and still use Vuex to serve up your data.

When to use A: Go Through Vuex Use A if you require faster experience, since there's no waiting for the server. You must be okay with optimistically displaying changes before actually saving. The other benefit is you can sync Vuex to localStorage. We use this for user preferences that are used to customize views. Its a poor experience to slow down the page just to wait on fetching those.

How to use A: Go through Vuex There's a couple ways to do this. Here's one pattern:

  1. Dispatch Vuex Action 1 from component
  2. Commit Vuex Mutation from Action that updates state - this is an optimistic update as you're assuming it'll go through
  3. Dispatch another Vuex Action 2 from Action 1 - This assumes you'll reuse this Action in multiple Actions, otherwise it can all go in Action 1
  4. Action 2 sends data to server
  5. Upon promise return, Action 2 commits mutation to update Vuex state
  6. Mutation needs to handle any discrepancies (or errors)

Count the cost of Vuex

Like your comment shows, its good to count the cost if you need to use Vuex at all because it does add a lot of overhead and complexity. The ideal is to write your components in such a way as to contain all interactions with one type of data (such as 'Lists') so you do not have to share state through Vuex.

7
  • 1
    I don't understand 5. If step 4 is successful, I don't need step 5 as I already updated the state in step 2. And if step 4 fails, then it seems that's what step 6 is referring to (ie, show an error message, revert the change, etc.) So I'm confused what step 5 means.
    – Yehosef
    Mar 5, 2018 at 0:01
  • 5
    when in doubt.. use vuex
    – pscheit
    Aug 11, 2018 at 20:01
  • 1
    A good UX will show the optimistic update in de UI and, if something fails with the backend, revert the change and show a warning to the user.
    – echefede
    Dec 10, 2018 at 14:08
  • 2
    @echefede This really depends on the importance of the data. On business applications where data is important, it is often better to make the user wait to KNOW it was properly saved. They may look at the optimistic UI update and think it was loaded and navigate away before getting an error. Again, it depends how important it is that the user needs to be assured it saved. Dec 13, 2018 at 16:55
  • 7
    It's also worth pointing out that Option A is also pretty much necessary if you want to support an offline workflow. Store everything locally, sync when the network is available.
    – Paul
    Jan 3, 2019 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.