mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Add $.fn.preempt, which runs the given event handler before any others.
This commit is contained in:
25
spec/stdlib/jquery-extensions-spec.coffee
Normal file
25
spec/stdlib/jquery-extensions-spec.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
$ = require 'jquery'
|
||||
|
||||
describe 'jQuery extensions', ->
|
||||
describe '$.fn.preempt(eventName, handler)', ->
|
||||
[returnValue, element, events] = []
|
||||
|
||||
beforeEach ->
|
||||
element = $("<div>")
|
||||
events = []
|
||||
|
||||
element.on 'foo', -> events.push(1)
|
||||
element.preempt 'foo', ->
|
||||
events.push(2)
|
||||
returnValue
|
||||
element.on 'foo', -> events.push(3)
|
||||
|
||||
it 'calls the preempting handler before all others', ->
|
||||
element.trigger 'foo'
|
||||
expect(events).toEqual [2,1,3]
|
||||
|
||||
describe 'when handler returns false', ->
|
||||
it 'does not call subsequent handlers', ->
|
||||
returnValue = false
|
||||
element.trigger 'foo'
|
||||
expect(events).toEqual [2]
|
||||
@@ -14,3 +14,10 @@ $.fn.scrollRight = (newValue) ->
|
||||
|
||||
$.fn.containsElement = (element) ->
|
||||
(element[0].compareDocumentPosition(this[0]) & 8) == 8
|
||||
|
||||
$.fn.preempt = (eventName, handler) ->
|
||||
@on eventName, (e) ->
|
||||
if handler() == false then e.stopImmediatePropagation()
|
||||
|
||||
handlers = @data('events')[eventName]
|
||||
handlers.unshift(handlers.pop())
|
||||
|
||||
Reference in New Issue
Block a user