Category Archives: QML

Flux: Qt Quick with unidirectional data flow

Hello !

Today is an important day, is the day of the first article of Qt I wrote.

Have you ever heard anything about Model View Controller or Model View Delegate???? Yes obviously, but right now, we’re going to talk about another thing (yes I am funny I know) . We are going to talk about Facebook, I mean a pattern which comes from Facebook.

What are we going to talk about??

We are going to talk about the Flux pattern, this pattern says data flow should be unidirectional as opposed to the Model View Delegate pattern.

modelview-overviewModel View Delegate multi directional data flow : Credit Qt

Flux pattern representation

Flux unidirectional data flow

What is the advantages to use Flux pattern?

  1. Signals propagation is easy and do not require any copy and paste.
  2. The code is easy to read.
  3. There is low coupling

What is the Action Creator?

When a user wants to interact with the application, he want to do an “Action“. For example, he could wants to add things to a todo list, so he could launch an Action(“Things to do”);
The Action Creator is here to give Action to our dispatcher.

What is the Dispatcher?

A dispatcher takes an action and its arguments and dispatchs it through all stores.

What is the Store?

A store is like a collection of datas but it also has logic buried inside it.

What is the View?

It shows all data.

What are we going to see?

We are going to see how to write a little application using Flux.
Our application could seem to that :
Screenshot_2016-02-19-20-22-20

Let’s code !

First, we need to know what our application has to do.
It must have possibility to add a counter, increment and decrement them. We exactly have 3 actions. It is that simple !

Now, we are going to see what is the AppDispatcher.

A dispatcher should take as argument the action type and a message (id for example). This dispatcher should dispatch this action as well.

You easily could improve the dispatch behaviour. Indeed, it could be more safe to use a queue… But in this example, I just show the mechanism and try to don’t overcomplicate the app.

I remind dispatcher dispatchs through the stores.
A store is a singleton which manages all objects of the same type. Our store manages all counters in the app.

A counter is an object with an id and a value, knowing that, we easily get :

On the onDispatched check if it is the good action, and if it is, do the required work.

Now we just need a view, as you see before, we use a “model” to store  all data, it will be the same for the view / delegate, but even if we use model view delegate, we will keep a unidirectional data flow.

The delegate will explains how an item should be rendered.
It is componed by 2 buttons (+ and -) and a value :

Yeah I know, there is some duplication of code, it is not good…
Now, we have the possibility to render items, we should print many of them.
Flux tells us datas are coming from Store, so let’s implement what Flux says!

It is the end, if you have any questions, please, let me know !
Hope you enjoyed it and learned somethings !

References

Flux by Facebook
Quick Flux : Problems about MVC and introduction

Thanks !