Skip to content

Missing guide for the get/set owner migration from @ember/application to @ember/owner #1418

Description

@NullVoxPopuli

Here is a path for libraries to support both (with typescript):

import { appEmberSatisfies, importSync, macroCondition } from '@embroider/macros';

import type Owner from '@ember/owner';

interface CompatOwner {
  getOwner: (context: unknown) => Owner | undefined;
  setOwner: (context: unknown, owner: Owner) => void;
}

export const compatOwner = {} as CompatOwner;

if (macroCondition(appEmberSatisfies('>=4.12.0'))) {
  // In no version of ember where `@ember/owner` tried to be imported did it exist
  // Using 'any' here because importSync can't lookup types correctly
  compatOwner.getOwner = (importSync('@ember/owner') as any).getOwner;
  compatOwner.setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
  // Using 'any' here because importSync can't lookup types correctly
  compatOwner.getOwner = (importSync('@ember/application') as any).getOwner;
  compatOwner.setOwner = (importSync('@ember/application') as any).setOwner;
}

// usage
compatOwner.getOwner(this)

and for app devs, here is how they can use libraries that use @ember/owner, even if they're behind 4.11 (when @ember/owner landed in stable)

https://github.com/NullVoxPopuli/ember-polyfill-get-and-set-owner-from-ember-owner

Requires:

  • @embroider/macros 1.19.4 (which does not require embroider)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions