diff --git a/addon/index.js b/addon/index.js index 21b8c7d..bf877dc 100644 --- a/addon/index.js +++ b/addon/index.js @@ -1,4 +1,5 @@ import MousetrapRoute from 'ember-mousetrap/mixins/mousetrap-route'; +import MousetrapView from 'ember-mousetrap/mixins/mousetrap-view'; import mousetrap from 'ember-mousetrap/macros/mousetrap'; -export { MousetrapRoute, mousetrap }; +export { MousetrapRoute, MousetrapView, mousetrap }; diff --git a/addon/mixins/mousetrap-view.js b/addon/mixins/mousetrap-view.js new file mode 100644 index 0000000..4299547 --- /dev/null +++ b/addon/mixins/mousetrap-view.js @@ -0,0 +1,36 @@ +import Ember from 'ember'; + +export default Ember.Mixin.create({ + mergedProperties: ['shortcuts'], + + didInsertElement: function() { + this._super.apply(this, arguments); + if (!this.shortcuts) return; + + Ember.keys(this.shortcuts).forEach(function(key) { + var callback = this.shortcuts[key]; + + if (!callback.__ember_mousetrap__) return; + + var keys = callback.__ember_mousetrap__.keys; + var action = callback.__ember_mousetrap__.action; + + Mousetrap.bind(keys, callback.bind(this), action); + }, this); + }, + + willDestroyElement: function() { + this._super(); + if (!this.shortcuts) return; + + Ember.keys(this.shortcuts).forEach(function(key) { + var callback = this.shortcuts[key]; + + if (!callback.__ember_mousetrap__) return; + + var keys = callback.__ember_mousetrap__.keys; + + Mousetrap.unbind(keys); + }, this); + } +});