Skip to content

Automatic state storage #158

Closed
Closed
@piknik

Description

@piknik

Why not have state storage automatically integrated with Vuex? The way I was thinking of specifying it was like this:

// cart.js
export default {
    state: {
        items: [],
        status: 'ok' 
    },
    mutations: { ... },
    save: ['items', 'status'] || { all: true }
}

Overriding the default storage provider:

// save-override.js
export default {
    load(module, property) { // invoked on page load
        return JSON.parse(localStorage.getItem(`${module}.${property}`))
    },
    save(state, module, property) {
        localStorage.saveItem(`${module}.${property}`, state[module][property])
    }
}

Saving through a middleware feels clunky somehow, like it doesn't fit with this specific purpose. A default storage implementation can be provided so only 1-liners are needed to enable saving/loading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions