Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not updating localStorage on repeateadly change #107

Open
SimplySayHi opened this issue Jun 6, 2017 · 3 comments
Open

not updating localStorage on repeateadly change #107

SimplySayHi opened this issue Jun 6, 2017 · 3 comments

Comments

@SimplySayHi
Copy link

Hi,
I tried to change repeatedly the radio from your demo ( http://garlicjs.org/#demonstration ) and printing in the console the localStorage.
I got that sometimes the value inside the localStorage is not updated and so, when I refreshed the page, I got the wrong radio to be checked.

Thanks,
Valerio

@SimplySayHi SimplySayHi changed the title not setting localStorage on repeateadly change not updating localStorage on repeateadly change Jun 6, 2017
@pacMakaveli
Copy link

pacMakaveli commented Sep 6, 2017

Can confirm. I've noticed it today as well on my form. This is actually quite critical because if the input is changed, on return it will return the old value that was stored, which sometimes can be incorrect.

After some debugging, I've come across this:

    persist: function () {

      // some binded events are redundant (change & paste for example), persist
      // only once by field val
      // 
      if (this.val === this.getVal()) { return }

      this.val = this.getVal();

      // if auto-expires is enabled, set the expiration date for future
      // auto-deletion
      // 
      if (this.options.expires) {
        this.storage.set(this.expiresFlag, (new Date().getTime() + this.options.expires * 1000).toString())
      }

      this.storage.set(this.path , this.getVal());
      this.options.onPersist(this.$element, this.getVal());
    }

Given that we have an input of type radio with Yes / No as value, the first 2 times the input is changed it returns undefined for this.val . Whereas after the input has been marked Yes or No, this.val is not undefined anymore, thus the function stops executing.

If I remove if (this.val === this.getVal()) { return } all is well. Is there a reason why this is in place?

I ran garlic.js against radio, checkbox, range, textarea and select with that check removed and it seems to work fine.

@guillaumepotier

@philwilks
Copy link

See #89. Try changing this to...

if (!this.$element.is('input[type=radio], input[type=checkbox]') && (this.val === this.getVal())) {
    return;
}

@guillaumepotier
Copy link
Owner

Hi @philwilks or @pacMakaveli,

Could you please confirm this is working with the suggested above change, and if so, submit a PR accordingly?

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants