Coder Social home page Coder Social logo

delegated-events's People

Contributors

aroben avatar dependabot[bot] avatar dgraham avatar josh avatar keithamus avatar koddsson avatar mislav avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

delegated-events's Issues

Generic constraint instead of any on the fire function

On the fire function the last parameter is any, if that was changed to a generic you could optionally use an interface to help with type checking and the behavior should remain the same as any. Maybe there's side effects I'm not realizing, but if there aren't any is there any objection to changing the definition to something like this?

export function fire<T>(target: Document | Element, name: string, detail?: T): boolean;

Example usage:

interface ISomeEvent {
  val1: int;
  val2: string;
}

// adds type checking to the event details
fire<ISomeEvent>(target, "do:something", {
  val1: 0,
  val2: "",
});

// remains the same as `any`
fire(target, "do:something", {
  val1: 0,
  val2: "",
});

t is not a constructor (using .min.js from jsdelivr 1.1.0)

I'm attempting to use this on my webpage (it's an internal company one) but the Chrome devtools keep coming up about 't is not a constructor'.

All i'm doing is:

window['delegate-events'].on('click', '.btn-delete', function(e) { console.log(e); });

.btn-delete is a button on a dynamic table row, the error occurs when I load the page, if I then click on the button it does nothing.

Support capture events

Feature request!

Would ❤️ support for capture events.

on('submit', 'form.js-validate-submission', (event) => {
  // ...
}, {capture: true})

or maybe we're getting kinda long on arguments, support named arguments for the full API:

on({
  event: 'submit',
  selector: 'form.js-validate-submission',
  capture: true
}, (event) => {
  // ...
})

Capture events would run before directly bound event handlers.

on('submit', 'form', (event) => {
  console.log(1)
}, {capture: true})

observe('form', (form) => {
  form.addEventListener('submit', (event) => {
    console.log(2)
  })
})

on('submit', 'form', (event) => {
  console.log(3)
})

Maybe I'll have time to write up a PR, but figured I'd make an issue first.

CC: @dgraham @mislav

How to set a delegated event on a given element and not globally?

In jQuery, I can set a delegated event handler to a specific element in the DOM tree:

$element.on('click', '.selector', callbackFunc)

How do I do the same with delegated-events?

In the source code I see this:

function on(name, selector, fn) {
  // ...
  document.addEventListener(name, dispatch, capture);
  // ..
  }

Does it mean event listeners are always/only attached to the document?

On does not intercept fire events

on('body', 'init', e => {
  console.log(e)
})
fire(document.body, 'init')

console.log is never triggered.

delegate-it works fine:

import on from 'delegate-it'
import { fire } from 'delegated-events'

on('body', 'init', e => console.log(e))
fire(document.body, 'init')

Attaching events to child iframes

I'd like to be able to attach events within a child iframe from the parent document js code? This doesn't seem possible in the current implementation. Thoughts?

0.3.0 dist depends on babelHelpers

define(['exports', 'selector-set'], function (exports, _selectorSet) {
  'use strict';

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.on = on;
  exports.off = off;
  exports.fire = fire;

  var _selectorSet2 = babelHelpers.interopRequireDefault(_selectorSet);

It looks like the generated output includes a reference to a babelHelpers helper module which github doesn't use. It shouldn't be necessary or an implicit dependency of delegated-events.

To: @dgraham

Getting access to the fired event object

The fire utility triggers a cancellable CustomEvent. But as it does not return the event object, there is no way to know whether the event was cancelled or no.

How to use in browser?

Is there a possibility to use delegated-events in an old-school way? Something like this:

<script src="/path/to/delegated-events.js"></script>
window.on(...)
window.off(...)
window.fire(...)

Form submit event

Is there any way to get this to work with a form submit?

I thought it might be a simple matter of using the custom event side of things, but even after trying that my form wasn't submitting.

I am doing the following:

window['delegate-events'].on('form:submit', document.querySelector('#myForm'), function(e) { e.preventDefault(); console.log('Form submitted!') });

If I then go to the developer tools console and do

window['delegate-events'].fire(document.querySelector('#myForm'), 'form:submit', {});

It returns 'true', but the actual event never triggers ('Form submitted' doesn't appear in the console).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.