Created
August 21, 2012 03:57
-
-
Save charlesjolley/3411380 to your computer and use it in GitHub Desktop.
Adding event locks to Ember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example view implements a simple dragging for mouse events. | |
Wall.DevView = Ember.View.extend | |
mouseDown: (ev) -> | |
ev.dispatcher.lock @, 'mouseMove', 'mouseUp' | |
@_mouseDown = @$().offset() | |
@_mouseDown.pageX = ev.pageX | |
@_mouseDown.pageY = ev.pageY | |
@_mouseDown.dispatcher = ev.dispatcher | |
console.log 'mouseDown' | |
mouseMove: (ev) -> | |
return unless @_mouseDown | |
left = @_mouseDown.left + ev.pageX - @_mouseDown.pageX | |
top = @_mouseDown.top + ev.pageY - @_mouseDown.pageY | |
@$().offset { left: left, top: top } | |
console.log 'mouseMove' | |
mouseUp: (ev) -> | |
return unless @_mouseDown | |
@_mouseDown.dispatcher.unlock 'mouseMove', 'mouseUp' | |
left = @_mouseDown.left + ev.pageX - @_mouseDown.pageX | |
top = @_mouseDown.top + ev.pageY - @_mouseDown.pageY | |
@$().offset { left: left, top: top } | |
@_mouseDown = null | |
console.log 'mouseUp' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add ability to lock focus on a view for specific events. | |
# Call before you create your application instance. | |
Ember.EventDispatcher.reopen | |
_bubbleEvent: (view, evt, eventName) -> | |
view = @_locks[eventName] if @_locks?[eventName] | |
evt.dispatcher = @ | |
@_super view, evt, eventName | |
lock: (view, eventNames...) -> | |
@_locks = {} if not @_locks | |
eventNames.forEach (eventName) => @_locks[eventName] = view | |
unlock: (eventNames...) -> | |
return if not @_locks | |
eventNames.forEach (eventName) => delete @_locks[eventName] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ember just sends events to whatever view your mouse or finger happens to be under. This causes really annoying breakage when trying to drag views around on the screen. This monkey patch adds the ability for an EventDispatcher to 'lock' delivery of some events to a particular view, so you can solve this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment