jQuery event off tests
You can't detach an event via the handler method if it's using .bind to attach context. jQuery can't attach it's guid internal tracker to the original method.
by Zmetser
JavaScript
$window = $ window
method = -> console.log 'foo'
console.info('-- Test with vanilla method --')
$window.on 'test', method
console.info 'Event registered', ($._data $window.get 0, 'events').events.test[0].guid
console.info 'Should log foo'
$window.trigger 'test'
$window.off 'test', method
console.info 'Should not log foo'
$window.trigger 'test'
$window.off 'test'
console.info('-- Test with bind --')
method2 = -> console.log 'foo'
$window.on 'test', method2.bind window
console.info 'Event registered', ($._data $window.get 0, 'events').events.test[0].guid
console.info 'Should log foo'
$window.trigger 'test'
$window.off 'test', method2
console.log method2.guid
console.info 'Should not log foo (unbind w/o bind)'
$window.trigger 'test'
$window.off 'test'
console.info('-- Test with bind --')
method2 = -> console.log 'foo'
bindedMethod = method2.bind window
$window.on 'test', bindedMethod
console.info 'Event registered', ($._data $window.get 0, 'events').events.test[0].guid
console.info 'Should log foo'
$window.trigger 'test'
$window.off 'test', bindedMethod
console.log bindedMethod.guid
console.info 'Should not log foo (unbind w/o bind)'
$window.trigger 'test'
$window.off 'test'
console.info('-- Test with proxy --')
method3 = -> console.log 'foo'
$window.on 'test', $.proxy method3, window
console.info 'Event registered', ($._data $window.get 0, 'events').events.test[0].guid
console.info 'Should log foo'
$window.trigger 'test'
$window.off 'test', method3
console.log method3.guid
console.info 'Should not log foo (unbind w/o $.proxy)'
$window.trigger 'test'