GithubHelp home page GithubHelp logo

christophrumpel / vue-save-state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spatie/vue-save-state

0.0 2.0 0.0 26 KB

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

Home Page: https://spatie.be/opensource

License: MIT License

JavaScript 100.00%

vue-save-state's Introduction

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

Latest Version on NPM Software License Build Status 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 storage.

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install

You can install the package via yarn:

$ yarn add vue-save-state

or npm

$ npm install vue-save-state

Usage

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

import saveState from 'vue-save-state';

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.

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

import saveState from 'vue-save-state';

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.

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

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

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

vue-save-state's People

Contributors

freekmurze avatar sebastiandedeyne avatar drbyte avatar

Watchers

James Cloos avatar Christoph Rumpel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.