Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[email protected] component templates in an addon

I'm wondering what the convention to use is when creating a component inside an addon project... If I generate a component in my addon project using [email protected], the blueprint will create a js file in addon/components, a template in addon/templates/components, and a js file in app/components. The part I'm not real clear about is where templates should live for these components. If my component template requires a partial, I need to put the partial template in the app/templates directory. If it lives in the addon/templates directory, it can't be resolved. So the question is this: Is it best to put all the templates (the component template and the partials) in the app/templates directory or leave the component template in the addon/templates/components directory and the partial in the app/templates directory? The latter feels slightly disorganized and the former seems more correct only because of the behavior of the blueprint. Anyone have any insight?

Thanks in advance.

like image 457
Ryan Connolly Avatar asked Mar 26 '15 17:03

Ryan Connolly


Video Answer


1 Answers

Ember-cli is under heavy development so a lot of the file structure is likely to change soon, but here on some insights on the current state of the folder structure and why it is arranged the way it is:

The app/ folder is what gets directly imported into your application. Helpers are pulled from here, which is why you have to have a file for each of your components in this folder. Additionally templates will get pulled from the main application here, and as such they will be accessible in the ways that templates are normally accessible in an ember app (render, partial, and standard resolution).

Some people choose to place all of their components code in app/, but this is a bad idea because the addon/ folder exists not only as a separation of your addons code, but as a way for it to be imported using ES6 imports. So, while you can't directly access the components under addon/components/, you can import them into your application like so:

import SomeComponent from 'some-addon/components/some-component'

This is very useful for addon consumers if they want to extend an addon to add some functionality.

Templates in addon get precompiled in the build tree, which makes addons a bit more robust (for instance if they are using a different version of htmlbars they will still be compatible with the base app). However, they are not accessible via the resolver, so you have to import them manually in your addon's components, which is why the blueprint for addon components looks like the following:

import Ember from 'ember';
import layout from '../templates/components/some-component';

export default Ember.Component.extend({
  layout: layout
});

Styles for addons can either be placed in addon/styles/ or app/styles/. In addon/styles/ they are also precompiled and included in the application by default. I highly recommend including styles in app/styles because this makes them accessible using preprocessor imports in the base application:

@import some-addon/main.css

This makes them completely optional to users of the addon, without resorting to app.import and config trickery (which is good because nested addons _do not support app.import. Don't use it.)

NOTE: They are not automatically namespaced, so you should put your styles in a folder to make sure they aren't conflicting with other addons styles.

In summary:

  • Any addon code that does not need to be directly accessible by the base app via helpers, initializers, etc. Should live in addon/
  • Anything you want to be accessible by ES6 imports should live in addon/
  • Templates should live in addon/templates/ and be imported manually
  • Component stubs, initializers, and other files that should be included in the standard Ember build chain should live in app/
  • Styles should live in app/styles/ and should be namespaced in a folder (e.g. app/styles/some-addon/)
  • Don't use app.import.
like image 134
pzuraq Avatar answered Oct 26 '22 04:10

pzuraq