Skip to content

Instantly share code, notes, and snippets.

@brian-mann
Last active March 30, 2017 05:50
Show Gist options
  • Select an option

  • Save brian-mann/5430655 to your computer and use it in GitHub Desktop.

Select an option

Save brian-mann/5430655 to your computer and use it in GitHub Desktop.
keyboard shortcuts in marionette views with mousetrap lib
_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"
@d3chapma
Copy link

I ended up doing this as a work around:

delegateEvents: (events) ->
      _delegate.call(@, events)
      bindShortCuts = _.bind(@bindShortCuts, this)
      _.delay bindShortCuts, 100

Don't perticularly like using a delay, but it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment