How to unbind an event defined by a global function?

I have an event binded to a global function (I need it to keep track of the function itself).

e.g.

 window['mUpEventHandler'] = function(){
      // ...
 }

It's binded to $('body') via

 .on('mouseup')

Now I know that I can unbind it knowing the name of the handler as in

 $('body').off(myHandler) // # myHandler = mUpEventHandler  for example

However when defining it globally, I have the function itself but not its name.

How do I overcome this?

Answers


Can you bind using a namespace. Then remove the event based off the namespace?

https://css-tricks.com/namespaced-events-jquery/

window['mUpEventHandler'] = function () {
  alert('test');
}


$('body').on('mouseup.mUpEventHandler', function () {
  mUpEventHandler()
});


$('button').click(function () {
  $('body').off('.mUpEventHandler');
})

http://jsfiddle.net/SeanWessell/5p7t22mh/


In $('body').off(myHandler), myHandler is not a "name", it's a variable that references a function.

To unbind an event handler you need a reference to the function. So

$('body').off(mUpEventHandler)
// or
$('body').off(window.mUpEventHandler)

will work just fine.


Need Your Help

Relational database regarding two tables

mysql database

Is it advisable to have table A having two foreign keys from table B? My thinking is that if one of the foreign key in table A is a primary key, then there is no need to add additional foreign key in

C where to define a mutex in a multithread program?

c multithreading unix mutex semaphore

I am working on a multi-thread program, and have a question about where to define the mutex.