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.

Unbind specific callbacks

See original GitHub issue

If there are multiple callbacks bound to a given key, e.g.

Mousetrap.bind('a', callback1)
Mousetrap.bind('a', callback2)

then using unbind nukes both of them. it’d be great to be able to unbind just one callback, e.g.

Mousetrap.unbind('a', callback2)

This seems like pretty common functionality – you can see it in jquery’s off, and in the DOM’s removeEventListener, etc.

I’m happy to write the feature and submit a pull request if this is something other people want too!

Issue Analytics

  • State:open
  • Created 11 years ago
  • Reactions:15
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
59nagacommented, Jan 25, 2016

I’ve used a Mousetrap new instance as a namespace.

var mousetrap1= new Mousetrap
mousetrap1.bind('a', callback1)

var mousetrap2= new Mousetrap
mousetrap2.bind('a', callback2)
mousetrap2.reset()// then available the mousetrap1

But, Mousetrap.trigger doesn’t notify to all instances. (Mousetrap.trigger is not a global)

Mousetrap.trigger('a') // no callbacks

+1 This feature seems still necessary also.

6reactions
ccampbellcommented, Jan 17, 2013

Mousetrap actually doesn’t even allow binding of multiple callbacks right now. The second ‘a’ will overwrite the first one. So even before calling unbind, callback1 will never be called.

I feel like having multiple callbacks tied to the same key would be strange. Can you think of a use case where you would want to do that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

.unbind() | jQuery API Documentation
A string containing one or more DOM event types, such as "click" or "submit," or custom event names. handler. Type: Function( Event eventObject...
Read more >
Bind / Unbind inside callback - jquery - Stack Overflow
What i'm trying to do is add the css for two div's... one that is a wrapper div which will slide out when...
Read more >
jQuery unbind() Method - W3Schools
How to use the unbind() method to unbind a specific function from a specified event for an element. ... Specifies an event object...
Read more >
Binding and unbinding of event handlers - plainJS
Binding and unbinding of event handlers. How to attach or detach a handler function to an event, such as click, focus, or submit....
Read more >
Backbone.js
If no event is specified, callbacks for all events will be removed. ... by telling it to remove just the events it's listening...
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