npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-save-state-session

v1.3.0

Published

A Vue mixin to save the state of a component to local or session storage

Downloads

25

Readme

A Vue mixin to save the state of a component to local or session storage

Latest Version on NPM Software License npm

This package provides a SaveState mixin that automatically saves any change in the state of your component to localStorage. The next time that component gets initialized it will restore its state from the saved values in local or session storage.

Install

You can install the package via npm:

npm install -save vue-save-state-session

Usage

In order to save the state of your component you'll need to add the SaveState mixin:

import saveState from 'vue-save-state-session';

export default {

    mixins: [saveState],

    ...
}

Next you'll need to add a method called getSaveStateConfig:

import saveState from 'vue-save-state';

export default {

    mixins: [saveState],
    
    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'cacheKey': 'nameOfYourComponent',
            };
        },
    },
}

With these steps done any change to the state of your component will get written to local storage. The value given in cacheKey determines to which key in local storage the state of this component will get written. When the component is created it'll restore its state from local storage.

Only save certain properties of the state

There's also a configuration option to determine which properties of the state should be saved/restored:

import saveState from 'vue-save-state-session';

export default {
    
    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'cacheKey': 'nameOfYourComponent',
                'saveProperties': ['title', 'text'],
            };
        },
    },
}

With this configuration only the title and text properties of your state will get saved/restored.

Alternatively you could specify which fields to ignore:

import saveState from 'vue-save-state-session';

export default {

    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'cacheKey': 'nameOfYourComponent',
                'ignoreProperties': ['title'],
            };
        },
    },
}

In this scenario all fields except for the title property in your state will get saved/restored.

Transforming the state on load

If you want to transform the values stored in local storage before loading the into the state of the component add an onLoad function to the object return by getSaveStateConfig.

import saveState from 'vue-save-state-session';

export default {

    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'onLoad': (key, value) => {
                    //return a new value
                },
            };
        },
    },
}

Use sessionStorage instead of localStorage

import saveState from 'vue-save-state-session';

export default {

    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'cacheKey': 'nameOfYourComponent',
                'cacheType': 'sessionStorage',
            };
        },
    },
}

Disable

Add dontSaveState property if you wish to temporarily disable the saveState function

import saveState from 'vue-save-state-session';

export default {

    // ...

    methods: {

        getSaveStateConfig() {
            return {
                'cacheKey': 'nameOfYourComponent',
                'cacheType': 'sessionStorage',
                'dontSaveState': true
            };
        },
    },
}

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ npm run test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact Freek Van der Herten instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.