Skip to content

Multiple handlers for the same event #23

Description

@AlienEngineer

When we need to execute the $().cssParentSelector(); more than once (e.g. some dynamic content is built, or some html is received from the server). We end up with multiple handlers for the same event therefore toggling in pair so it won't do nothing.

To solve this i used an annotation in the element to it won't be affected more than once to the same event.

!state ? toggleFn() : $(this).on(this_state, toggleFn);

Replaced with

// If there's no subject then don't register the handler on the event observer.
if ($(subject).size() === 0) {
    return;
}

var $this = $(this);
var this_state = stateMap[state] || state;

if (($this.attr('data-parent-selector') || "").indexOf(this_state) === -1) {
    $this.attr('data-parent-selector', ($this.attr('data-parent-selector') || "") + this_state);
    !state ? toggleFn() : $(this).on(this_state, toggleFn);
}

This could be improved because if two rules (e.g. two ! > :focus rules) are declared only one will be affected. For now this solves my problem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions