Created
July 21, 2017 18:46
-
-
Save alexsasharegan/ffef9f1d953a1b71be42c461266fcbb0 to your computer and use it in GitHub Desktop.
Explanation of tweet: https://twitter.com/AlexSashaRegan/status/888439217931419649
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Store } from "vuex" | |
| export default new Store({ | |
| state: { | |
| models: [], | |
| }, | |
| getters: { | |
| // Caches model references into an object keyed by model id's | |
| modelsById (state) { | |
| const dictionary = Object.create(null) | |
| return state.models.reduce((dict, model) => { | |
| dict[model.id] = model | |
| return dict | |
| }, dictionary) | |
| } | |
| }, | |
| mutations: { | |
| UPDATE_MODEL_PROPS (state, {id, props = {}}) { | |
| // Currently no access to getters, so we must find the model ourselves. | |
| // Waste of cycles if the list is large. | |
| const model = state.models.find(m => m.id === id) | |
| // Alternately, if we had the getters: | |
| const model = getters.modelsById[id] | |
| if (model) { | |
| Object.assign(model, props) | |
| } | |
| }, | |
| }, | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment