IcarusWorks / ember-key-manager

A service for (un)binding keyboard up and down events.
MIT License
42 stars 11 forks source link

Callbacks on a controller aren't triggered (only on load) #21

Closed gossi closed 6 years ago

gossi commented 6 years ago

I do use this addon https://github.com/kellyselden/ember-controller-lifecycle for a setup() and reset() method on my controllers. I do use this methods to register and deregister keyboard shortcuts. I can see they register or deregister (I log them) when I load the route (cmd + r) or transition in rsp out. Although they are registered, they aren't invoked, when transitioning in. They are only invoked, when the route they are registered on is directly (re)loaded.

jordpo commented 6 years ago

Can you please expand on exactly the issue you are having and paste a code snippet?

gossi commented 6 years ago

Yup, here is my code from my controller (with setup() and reset() from the mentioned addon):

setup() {
    console.log('register keys');
    const manager = this.get('keyManager');

    // space
    manager.register({
        name: 'toggle',
        keys: ['space'],
        downCallback: () => {
            console.log('downCallback invoked');
            this.togglePlay();
        }
    });
},

reset(isExiting) {
    if (isExiting) {
        console.log('deregister keys');
        const manager = this.get('keyManager');
        manager.deregister({ name: 'toggle' });
    }
}

If I navigate to this route (clicking links in my app), I see the console statements, yet the callbacks don't work. If I stay on the route and press reload (cmd + r) the callbacks are invoked. If I then start navigating in my app again (clicking links forth and back) I see the console.log statements for registration and deregistration respectively, yet the callbacks aren't invoked.

jordpo commented 6 years ago

@gossi I'm not familiar with the library you are using but using the Ember native lifecycle hooks on the route file does register the key handlers correctly.

import Route from '@ember/routing/route';
import {
  inject as injectService,
} from '@ember/service';

export default Route.extend({
  keyManager: injectService(),
  setupController(controller, model) {
    this._super(controller, model);

    const manager = this.get('keyManager');
    manager.register({
      name: 'toggle',
      keys: ['space'],
      downCallback: () => {
        console.log('downCallback invoked');
      },
    });
  },

  resetController(controller, isExiting) {
    if (isExiting) {
      const manager = this.get('keyManager');
      manager.deregister({ name: 'toggle' });
    }
  }
});
gossi commented 6 years ago

I checked back on this behavior with current v0.2.2. It kinda works... I'm still working with controller lifecycles, all I did was updating the code above to the new macro API. What I can see, that whenever transitioning into a route that registers a macro, I must click somewhere on that page to "activate" everything. Dunno, if this is loosing focus or something.

However, loading the page (on a specific page that has registered macros, using browser reload) works. Transitioning out an back into that route needs a click. It doesn't matter whether I register the macro within a controller/route or a component makes no difference here.

gossi commented 6 years ago

Has been fixed with v0.2.3 - you guys are amazing :)

jordpo commented 6 years ago

Awesome - @patrickberkeley has been a beast :)