Last active
March 30, 2017 05:50
-
-
Save brian-mann/5430655 to your computer and use it in GitHub Desktop.
keyboard shortcuts in marionette views with mousetrap lib
This file contains hidden or 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
| _delegate = Backbone.Marionette.View::delegateEvents | |
| _close = Backbone.Marionette.View::close | |
| _.extend Backbone.Marionette.View::, | |
| delegateEvents: (events) -> | |
| _delegate.call(@, events) | |
| @bindShortCuts() | |
| bindShortCuts: -> | |
| for key, method of _.result(@, "shortCuts") | |
| method = @[method] unless _.isFunction(method) | |
| throw new Error("Method #{method} not found!") unless method | |
| method = _.bind(method, @) | |
| Mousetrap.bind(key, method) | |
| close: -> | |
| _close.call(@) | |
| @afterClose() | |
| afterClose: -> | |
| @unbindShortCuts() | |
| unbindShortCuts: -> | |
| for key of _.result(@, "shortCuts") | |
| Mousetrap.unbind(key) | |
| ## in your views somewhere | |
| class Show.View extends Marionette.ItemView | |
| shortCuts: | |
| "command+ctrl+c" : -> @trigger "clone:last:point" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ended up doing this as a work around:
Don't perticularly like using a delay, but it works