Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Created July 21, 2017 18:46
Show Gist options
  • Select an option

  • Save alexsasharegan/ffef9f1d953a1b71be42c461266fcbb0 to your computer and use it in GitHub Desktop.

Select an option

Save alexsasharegan/ffef9f1d953a1b71be42c461266fcbb0 to your computer and use it in GitHub Desktop.
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