0

When I run ember g component foo-bar in an Ember Addon project (let's say addon-project), it generates following:

// addon-project/addon/components/foo-bar.js
import Ember from 'ember';
import layout from '../templates/components/foo-bar';

export default Ember.Component.extend({
  layout
}

// addon-project/addon/templates/components/foo-bar.hbs
{{yield}}

// addon-project/app/components/foo-bar.js
export { default } from 'addon-project/components/foo-bar';

I noticed that it does not generate addon-project/app/templates/components/foo-bar.js to export the component template but explicitly link the template using layout.

Why not generate addon-project/app/templates/components/foo-bar.js? Is there a reason for this behavior?

Also why is layout imported using relative path instead of absolute path (i.e. import layout from 'addon-project/templates/components/foo-bar?

Sang Park
  • 382
  • 2
  • 17

1 Answers1

0

First - which version of ember-cli do you use? Blueprints depends on version

For me it do next

vvs ~/r/e/test-addon> ember -v
ember-cli: 2.6.3
node: 4.4.7
os: darwin x64
vvs ~/r/e/test-addon> ember g component test-component
Could not start watchman; falling back to NodeWatcher for file system events.
Visit http://ember-cli.com/user-guide/#watchman for more info.
installing component
  create addon/components/test-component.js
  create addon/templates/components/test-component.hbs
installing component-test
  create tests/integration/components/test-component-test.js
installing component-addon
  create app/components/test-component.js

so it generate template as well

Next - it use related path for import layour to decrease number of places which depends on addon namespace ( which is defined inside index.js - so if you change that namespace in future - you should update only files which explicit maped into application )

Oh. It didn't generate template bridge cause in component used layout. It allow you extend component without defining template ( you can just extend original component, add/change logic and skip creation template for it ) If you'll use template instead of layout - you should define template ( or use the same layout explicit ) manually

Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44
  • I tested both 2.4.3 and 2.7.0. Actually it didn't generate the bridge file for template for you either. It generated component, template, test for component, and and bridge for the component but NOT the bridge for template. I'm wondering why it didn't generate `test-addon/app/templates/components/test-component.js` file in your case – Sang Park Jul 28 '16 at 04:12
  • re: not depending on addon namespace... That logic doesn't make sense to me. 1) Why would you change your addon namespace? That doesn't seem like something you would or should do. And even if you have to, you can just "find & replace all". 2) using relative path is fragile. If you have multiple layers of organization, you will end up with "../../../../". And if you move around your file, you will have to make sure you have right number of ../ 3) And this is the only place I've seen EmberCLI use relative path. Even Ember Data uses 'ember-data' namespace internally, not relative path. – Sang Park Jul 28 '16 at 04:31
  • Oh. It didn't generate template bridge cause in component used layout. It allow you extend component without defining template ( you can just extend original component, add/change logic and skip creation template for it ) If you'll use template instead of layout - you should define template ( or use the same layout explicit ) manually – Vasiliy vvscode Vanchuk Jul 28 '16 at 08:08
  • ok.. i guess that makes a bit more sense... but that break the pattern. Ember has always used matching name to match the `.js` and `.hbs` files. Overriding template using `layout` is a custom behavior (`layout` is not even listed in Component API doc). If this is the reasoning, it's weird that a custom behavior is supported by default. And only for Addon since this is not the case for components generated for an App – Sang Park Jul 28 '16 at 18:05
  • It does http://emberjs.com/api/classes/Ember.Component.html#property_layout The Idea is that for addon case you more often change logic, not template. it depends on version of ember-cli. In old versions it use template – Vasiliy vvscode Vanchuk Jul 28 '16 at 18:15
  • ah layout is a private property (which is just another red flag imo). I guess it is true that you would extend components in Addon more so than components that you created... but I still feel like this is a weak argument for this pattern. there are better way to handle that situation I think. – Sang Park Jul 28 '16 at 21:55
  • you can discuss it in ember RFCS =) – Vasiliy vvscode Vanchuk Jul 29 '16 at 06:56