question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is there a way to bind custom events on custom HTML element?

See original GitHub issue

Hi @kumilingus,

I’m trying to add a custom event on my custom HTML element using official demo from here https://github.com/clientIO/joint/blob/master/demo/shapes/html.html https://github.com/clientIO/joint/blob/master/demo/shapes/src/html.js

I’ve only made a few tweaks like the following:

in html.html

.my-html-element input,
.my-html-element button {
  pointer-events: auto;
}

In the html.js

  • Added a button element to the existing template
template: [
    '<div class="my-html-element">',
    '<label data-attribute="mylabel"></label>',
    '<input data-attribute="myinput" type="text"/>',
    '<button>Custom button</button>',
    '</div>'
].join(''),
  • Binding the events in paper
paper.on('element:pointerdown', (event) => {
    console.log('clicked on the element!');
});

paper.on('element:button:pointerdown', (event) => {
    console.log('clicked on the button');
});

A repro sample: https://codepen.io/JoshuaMilton/pen/povZpzv?editors=1111

Now, if you click on the element, the regular event will be triggered and logged out “clicked on the element!” But if you click on the button I added, the custom event won’t trigger. The problem here is I don’t know how to correctly bind these custom events on my custom HTML elements. I’ve googled and searched everything that I can possibly find but still cannot figure out how to do so 😅

Thank you for the help!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
konekoyacommented, Feb 7, 2020

@kumilingus Thank you so much for the help. This works perfectly 👍 💯

1reaction
kumilinguscommented, Jan 22, 2020

I see. The simplest way would be this:

// onRender();
// `this` is an elementView
$box.find('button').on('click', function() {
  this.notify('element:button:pointerclick', evt);
}.bind(this));

// later
paper.on('element:button:pointerclick', (elementView, evt) => {});

Note, this solution is fine as long as you don’t have thousands of elements. If there is a lot of elements in the graph, then there is also a lot of event handlers in memory. In that case, it is better to bind a single event handler.

var MyPaper = joint.dia.Paper.extend({

  events: Object.assign({
     'click button': 'onButtonClick'
   }, joint.dia.Paper.prototype.events)

  onButtonClick: function(evt) {
    // find view from evt.target
    // trigger `element:button:pointerclick` on the paper
  }
});

For the built-in event attribute, you need to add event attribute to your markup first <button event="element:button:pointerclick">Custom button</button>. It does not work though, I guess, it is because it is guarded by paper.options.guard. I need to investigate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How should I bind an event to DOM elements created with a ...
Right now, I'm binding events to the parent element of my custom tag's rendered content, then using classes to target the event onto...
Read more >
Using custom elements - Web Components | MDN
The controller of custom elements on a web document is the CustomElementRegistry object — this object allows you to register a custom element...
Read more >
Custom Elements, event listener and this - Jonas Fährmann
In an event listener this normally corresponds to the EvenTarget ( event.target ). The bind method is used to ensure that this corresponds...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
Notes: We should use addEventListener for our custom events, because on<event> only exists for built-in events, document. onhello doesn't work.
Read more >
JavaScript Custom Events - GeeksforGeeks
HTML · We create a custom event using the CustomEvent constructor. · This takes two arguments, the first is the name of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found