1

I'm builiding a local storage for my data for a Vue + Laravel SPA app.

The storage looks as such:

const Storage =
{
    handler:
    {
        get: function(o, prop)
        {
            return localStorage.getItem('storage.data.' + prop)
        },
        set: function(o, prop, value)
        {
            localStorage.setItem('storage.data.' + prop, value)
        }
    }
}

Vue.prototype.$storage = new Proxy(Storage, Storage.handler)

Fairly simple, I would say.

The issue I'm having is, I want the code below to create all neccessary objects as it goes through:

this.$storage.auth.token.kohen = 'test'

To put more bluntly, I want objects auth and token to be created when I set kohen, so I don't have to do:

this.$storage.auth = {}
this.$storage.auth.token = {}

The problem with that is, it runs get on auth first - which also returns undefined.

3
  • $storage.auth.token.kohen is not a property of $storage however $storage.auth is. Try to print what prop is to fully understand how the proxy works Commented Feb 14, 2020 at 9:45
  • You can also have a look at this solution on How to use javascript proxy for nested objects Commented Feb 14, 2020 at 9:47
  • @Vivick So, I should use proxies for nested objects aswell and just keep track of a parent? Commented Feb 14, 2020 at 9:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.