I am utilizing a storage provided by LokiJS to update the Vuex state through mutations in the autoloadCallback
.
The LokiJS storage setup:
var db = new loki('mydatabase', {
autoupdate: true,
autoload: true,
autoloadCallback: setupHandler
})
Managing Vuex state and mutations:
const state = {
ads: []
}
const mutations = {
updateArray(from, to) {
from = to
},
updateAds() {
state.ads = db.getCollection('ads').data
}
}
Exploring the callback function:
function setupHandler() {
setupCollection('ads') // Creates the collection if it doesn't exist
db.getCollection('ads').insert({dummy: "Dummmy!"})
mutations.updateArray(state.ads, db.getCollection('ads').data)
mutations.updateAds()
}
The problem arises when calling updateArray(state.ads, content)
as it does not update state.ads
to content
. On the other hand, updateAds()
function achieves the same outcome by dynamically updating state.ads
. What is the root issue with attempting to create a generic function like updateArray? Is there a work-around?
For a demonstration, check out this JSFiddle MCVE example showcasing this behavior.