Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Last active February 28, 2017 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickberkeley/3e4951dd71a7b25d6df372e1351ae09c to your computer and use it in GitHub Desktop.
Save patrickberkeley/3e4951dd71a7b25d6df372e1351ae09c to your computer and use it in GitHub Desktop.
`ember-keyboard-manager` example usages.
import Ember from 'ember';
const {
get,
inject,
} = Ember;
export default Ember.Component.extend({
keyManager: inject.service(),
didInsertElement() {
get(this, 'keyManager').register({
keys: ['escape'],
name: 'search-modal',
downCallback: run.bind(this, function() {
this.send('toggleModal');
}),
priority: 10,
});
},
willDestroyElement() {
get(this, 'keyManager').deregister({
name: 'search-modal', // This name must match the name the binding was registered with above.
});
},
actions: {
toggleModal(){
this.sendAction('toggleModalAction');
},
},
});
import Ember from 'ember';
const {
get,
inject,
} = Ember;
export default Ember.Route.extend({
keyManager: inject.service(),
actions: {
didTransition() {
this._super(...arguments);
get(this, 'keyManager').register({
keys: ['escape'],
name: 'fancy-route',
downCallback: run.bind(this, this._redirectToLaLaLand),
priority: 100,
});
},
willTransition() {
this._super(...arguments);
get(this, 'keyManager').deregister({
name: 'fancy-route',
});
},
},
// The `event` that's returned as a parameter here is the JS keyboard event.
_redirectToLaLaLand(event) {
if (event) {
event.preventDefault();
}
this.transitionTo('main.la-la-land');
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment