Existing component check in js file


Hi , i need check whether a component is already existing or not in one js file , is there any way how to do this ? whether this is feasible or not ?


I don’t fully understand what you mean by, “check whether a component is already existing or not in one js file”. If you’re referring to just checking to see if a component globally exists or not, you can use the “Owners API”. Here is an example:

import Ember from 'ember';

export default Ember.Component.extend({
  init() {
    let owner = Ember.getOwner(this);
    if (!owner.hasRegistration('component:xFoo')) {
      /* do something? */

The Owner API is newly added in Ember 2.3. If you’re using an older version of ember, you can use this polyfill. Also relevant docs ContainerProxyMixin and RegistryProxyMixin.


thanks for replying workmanw :slight_smile:

very true i wanted to check already created component globally , but i m using ember 1.11 , i m afraid this API wont work . i tried using following code


it is working fine . If component is not there , it will return as undefined .


A couple of notes.

  1. The polyfill that I linked will work with Ember 1.11. You should really consider using this because it will be one less thing to fix if you’re upgrading to Ember 2.x.

  2. If you’re going to use the container, you shouldn’t use lookup to test it because in some cases it will create a new instance of for that lookup. Instead use has, this.container.has('template:templateName');.