mardi 3 septembre 2019

Ember 3.12: Cancel subscription for event from service

The following used to work:

export default Route.extend({
  device: service(),

  activate() {
    this._super(...arguments);

    let device = get(this, 'device');
    device.one('change', this, this._onDeviceChange);
  },

  _onDeviceChange() {
    // ...
  },

  deactivate() {
    this._super(...arguments);

    let device = get(this, 'device');
    device.off('change', this, this._onDeviceChange);
    },
});

But "now" (in v3.12 here, most probably since a couple of versions) I get

Error: Assertion Failed: You attempted to remove a function listener which did not exist on the instance, which means you may have attempted to remove it before it was added.

By removing the device.off()-part the error is gone, but I wonder: Is off() optional? Will removing off() produce some kind of leak? Why does the listener "not exist on the instance" anyway?

PS: I tried to off the listener before calling _super, but no difference...




Aucun commentaire:

Enregistrer un commentaire