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

KnockoutSpa (router.ts) Event listener and non trivial a tags #273

@cmdkeen

Description

@cmdkeen

The code for the clickEventListener in router.ts does not capture the click event on a tags that contain child elements:

<a data-bind="attr: { href: url }"
    <span data-bind="text: text"></span>
</a>

Whereas the non tag version works fine.

<a data-bind="attr: { href: url }"
    <!-- ko text: url --><!-- /ko -->
</a>

Switching to use the jQuery event selector and using currentTarget fixes everything for me. There's probably a Vanilla JS way of achieving the same thing but jQuery is in the project already and it is a schoolnight.

this.clickEventListener = evt => {
    let target: any = evt.currentTarget;
    if (target && target.tagName === 'A') {
        let href = target.getAttribute('href');
        if (href && href.charAt(0) == '/') {
            history.push(href);
            evt.preventDefault();
        }
    }
};

$(document).on('click', 'a', this.clickEventListener);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions