From 19d318d5093e67258d66f3476383a987e0b56a3f Mon Sep 17 00:00:00 2001 From: Vishesh Joshi Date: Fri, 1 May 2015 14:17:24 -0700 Subject: [PATCH 1/2] Adding view mixin --- addon/mixins/mousetrap-view.js | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 addon/mixins/mousetrap-view.js 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); + } +}); From c2fe3068679dee64218a3f821baf6404a85fcb63 Mon Sep 17 00:00:00 2001 From: Vishesh Joshi Date: Fri, 1 May 2015 14:21:01 -0700 Subject: [PATCH 2/2] Adding View in index.js --- addon/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 };