Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Add support for form reset functionality. #154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Haprog
Copy link
Contributor

@Haprog Haprog commented Jul 28, 2014

Fix issue #111
Tokens are now automatically updated after form reset to reflect the new input value after reset.

Haprog added 2 commits July 28, 2014 17:39
Tokens are now automatically updated after form reset to reflect the new input value after reset.
@Haprog
Copy link
Contributor Author

Haprog commented Jul 29, 2014

I also made an enhancement to this that triggers a new custom event 'tokenfield:postreset' (as in "pre-/post-") within the timeout function after the value has been (potentially) changed due to form reset. This event is useful if you for example want to run your custom validation code after the value has been changed by form reset.

This is the enhanced implementation:

    // Update tokens on form reset
    this.$element.closest('form').on('reset', function (e) {
      var form = this;
      var oldValue = _self.$element.val();

      // Set a timeout to run code after the reset event is finished
      window.setTimeout(function(){
        var newValue = _self.$element.val();

        // If the value changed due to form reset then update tokens
        if (oldValue != newValue) {
          _self.setTokens(newValue, false, false);
        }

        var eventOptions = {
          attrs: {
            oldValue: oldValue,
            newValue: newValue
          },
          relatedTarget: form
        };
        var postResetEvent = $.Event('tokenfield:postreset', eventOptions);
        _self.$element.trigger(postResetEvent);
      }, 0);
    });

But I didn't publish this yet so if this is ok, I can either push it to my branch for this pull request or make a new pull request after this has been merged unless you want some changes to this.

@Haprog
Copy link
Contributor Author

Haprog commented Jul 29, 2014

I just realized that this event listener should probably be moved to Tokenfield.listen method and unbound in Tokenfield.destroy is this right?

I can make the changes if you confirm that this is how it should be done.

@ragulka
Copy link
Member

ragulka commented Aug 1, 2014

@Haprog Good catch. This should definitely be in the listen method as you pointed out. Once you change that, I can merge this in. Would you mind taking a stab at writing a test for this as well?

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

Successfully merging this pull request may close these issues.

2 participants