Git Product home page Git Product logo

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.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Postcardware

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

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

All postcards are published 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.

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';

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';

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';

export default {

    // ...

    methods: {

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

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

adrianmrn avatar alexvanderbist avatar drbyte avatar freekmurze avatar sausin avatar sebastiandedeyne avatar ss44 avatar tjoosten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-save-state's Issues

New release

Hey! Can a new release be made? We noticed that #8 is missing from 1.2.0.

Cheers!

Nuxt.js compatibility

In the latest version of Nuxt.js (2.0), the plugin does not seem to function correctly.

Saving the state of a page, results in the page content being loaded then immediately updated with whatever is returned from the data function. My guess is that this is a Nuxt.js issue though. Anyone else having this issue?

Export localstorage

Hello, I would like to know if there are any plans to possibly export saved local storage data ?

Eg. I would like to save the local data as a JSON structure, that could possibly be imported somewhere else.

Case:
I am planning on letting individuals customize their own personal experience. This experience will only be available on their own browser, unless they decide to save it to the "cloud".

Otherwise, how would you advise I achieve this ?

Issue storing data

I am currently saving the below piece of data on my local storage.

showing: {
id: null,
data: {
answers: [],
clients: {},
incomes: [],
sessions: []
}
},

I don't want to store all the data that comes with showing, I just want to store showing.id. I tried to ignore showing.data but it doesn't work. Also, is there a way to add an expiration to the stored data?

Saving a property of an object

Is there a way to save and load property of an object in data? For example, if in data I am saving calendarOptions -- I only want to update certain properties of that. Not calendarOptions as a whole.

Local Storage cache key is `undefined`

Hey Team,

First off, thanks for this plugin! This will be really useful.

I've gone through the setup in the readme and have implemented in a Vue component in my application. The state is being saved to local storage, however, the key is being set to undefined. I am declaring a key in my component in this way:

export default {
  import saveState from 'vue-save-state';
  // ...
  mixins: [saveState],
  // ...
  methods: {
      getSaveStateConfig() {
        return {
          'vueProjectGrid': 'ProjectGrid',
        };
      },
  },
  // ...
}

And here's a screenshot from Chrome devtools:

screenshot 2017-07-26 11 26 03

Any help you could provide would be much appreciated.

Failing to transform the state on load

This is more a question about how the onLoad works, than an issue

I have a complex state in local storage and I want to replace it (the whole saved state) with another JSON that cames from a DB Ajax Request.

Sorry, I tried a lot of combinations in the onLoad function in getSaveStateConfig but none of them seemed to achieve what I want. Am I missing something ? Does the onLoad function support this? or is only for updating one key in the saved state, at a time ??

Thanks!!

after inject to my project

app.js:47580 [Vue warn]: Error in created hook: "TypeError: this.getSaveStateConfig is not a function"

found in

---> at resources\assets\js\pages\Admin\Users.vue

what a fack? help please!

Using mixin globally

Just wondering if there is a way to import the mixin globally. Since I will be using it in almost all my components I'd like to do something like this:

main.js

import Vue from 'vue'
import saveState from 'vue-save-state'
....
Vue.mixin(saveState)

Then in my components just declare the getSaveStateConfig method

getSaveStateConfig () {
            return {
                cacheKey: this.$options.name,
                saveProperties: this.preserveState ? ['layout', 'links', 'filter'] : []
            }
 },

Unfortunately, I'm getting TypeError: this.getSaveStateConfig is not a function

Any advice will be appreciated

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.