25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Principle #2 – Data is read-only<br />

Components can freely read data from the store. But they cannot change data in<br />

the store, at least not directly.<br />

Instead, they must inform the store of their intent to change the data <strong>and</strong> the store<br />

will be responsible for making those changes via a set of defined functions<br />

called mutator methods.<br />

Why this approach? If we centralize the data-altering logic then we don't have to<br />

look far if there are inconsistencies in the state. We're minimizing the possibility<br />

that some r<strong>and</strong>om component (possibly in a third party module) has changed the<br />

data in an unexpected fashion:<br />

Figure 8.3. State is read-only. Mutator methods are used to write to the store

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!