Generate components in sub-folders in ember/ember-cli
Asked Answered
G

3

10

Based on recommendations for the preparation for Ember 2.0...

• In general, replace views + controllers with components
• Only use controllers at the route level...

...we're supposed to eschew Controllers and Views in favor of Components. I haven't been able to figure out and/or understand how to generate Components that aren't direct parents of the components folder, i.e. components/component-name.js.

My current controllers folder looks something like:

/controllers
    /account
        index.js
        edit.js
    /business
        index.js

Basically, there are sub-folders that group logic based on the sections of the application. How do I accomplish this with just components?

Seeing that components must have a "-" in them, I tried, but get an error...

ember generate component account/index-module.js
You specified "account/index-module.js", but due to a bug in Handlebars (< 2.0) slashes within components/helpers are not allowed.

Do all components have to be like

components
    account-index.js
    account-new.js
    business-index.js

i.e. all in the same folder? This will start to get out of hand with the addition of what I actually consider to be components (things like video-viewer.js, text-editor.js, radio-button.js).

I would really like to have components in sub-folders, but unsure how to do this.

components
    /media
        /audio
            audio-player.js
        /video
            video-player.js
    /text-editing
        text-editor.js
        editor-toolbar.js

My components folder is already gross and I just got started:

enter image description here

Is it okay to leave the account/business logic in Controllers (seeing that it does say you should only use controllers at the Route level)?

I'm really confused about this "all components, all the time" convention.

Gorga answered 18/11, 2014 at 16:51 Comment(0)
M
7

Ok, so I had the same problem and as of ember 1.9-beta.3 (that's the version I tested). It is possible to have components nested under resource directories.

That means that you can have a "user" route or resource. And let's say you have a component which you only want to use with the user resource, so you want to put the component under the resource directory.

The way to do it is to put the component under the resource directory app/pods/user/component-name/template.hbs. The important part is to remember that components must have a dash in their name. It can't be just .../user/component it has to be .../user/component-name with a dash. Then you can use the component as {{user/component-name}} in your templates.

Also I think this is only possible when you're using the pod structure.

Marvamarve answered 26/11, 2014 at 12:58 Comment(0)
I
6

Ok, I think this question/answer needs a bit of an update for 2019. I have been using Ember for all of about a month, and my components folder has already become a pigpen. And the tutorial and main API docs don't really cover how to organize your components.

So I did a search of course. And the only answers I could find (like this one) are from around 2014-2015, and don't reflect modern Ember. I was about to accept my fate when I found this in the Ember syntax conversion guide. (Note to the Ember folks: This is an important issue, one that almost every new user will encounter. It should feature a bit more prominently in the documentation. Maybe not the tutorial, but definitely in Components section)

You can in fact generate components under a sub-folder in Ember as such:

$ ember generate component foo/bar-baz
installing component
  create app/components/foo/bar-baz.js
  create app/templates/components/foo/bar-baz.hbs
installing component-test
  create tests/integration/components/foo/bar-baz-test.js

So that's great, the files are created under components/foo and templates/components/foo. And to resolve the name of the component for use in another template, you can use either the old style syntax:

{{foo/bar-baz }}

Or the new style angle bracket syntax:

<Foo::BarBaz />
Intoxicated answered 5/7, 2019 at 19:55 Comment(1)
If you want to generate a component in its own folder you can run ember g component foo/indexProudfoot
N
1

As the assertion suggests this is due to Handlebars 1.x, and will be available soon.

Ember 1.9 beta builds currently support this, though I'm not positive if ember-cli's resolver would work with it right now. You can read more about Handlebars 2.0 here.

Using a pods structure will also help with organization, and I believe is going to be the recommended strategy going forward.

For now, I'd suggest not to worry about it! Remember the transition plan will be smooth, and as the official releases come out for Ember and Ember CLI, you'll get deprecation warnings.

Nootka answered 20/11, 2014 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.