Garlic.js icon indicating copy to clipboard operation
Garlic.js copied to clipboard

not updating localStorage on repeateadly change

Open SimplySayHi opened this issue 7 years ago • 3 comments

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 avatar Jun 06 '17 07:06 SimplySayHi

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

pacMakaveli avatar Sep 06 '17 13:09 pacMakaveli

See #89. Try changing this to...

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

philwilks avatar Aug 12 '19 14:08 philwilks

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

guillaumepotier avatar Sep 02 '19 12:09 guillaumepotier