What is needed to create an EmberJS component ? Do those use jQuery based plugins ? Also for usage, are they always used inside templates ?
Seriously. This transition from controllers to components makes things damn confusing. When we create a component, it creates the templates/components/component-name.hbs as well as a components/component-name.js
When is the JS file used for the component? Is it called on an ACTION in the component? It doesnāt appear soā¦ It seems like a controller is still necessary, or there is some secret magic that is required to make it not necessary. Do we use controllers or not?
A full component example, calling a RESTful service, using a model and actions, would be nice to see.
I changed my example to use components:
I hope it explains it some more.
Part of this confusion is because Components cannot completely replace Controllers, not until we have Routable Components.
Still use Controller (the base type, not ArrayController) and migrate when 2.1/2.2 hit.
I do not see any answer as to how do I create a component using EmberJS and actually use it !
Perhaps my example is not clear than. If you take a look at https://github.com/broerse/ember-cli-blog/tree/master/app/templates/components you see how blog-post.hbs
shows the blog-post-edit
component. Because there are no routable components yet you have to add the āblog-postā components to your routes like this: https://github.com/broerse/ember-cli-blog/blob/master/app/templates/post.hbs If there are routable components they donāt need to be inside templates.
I donāt fully understand this question but some components use jQuery.
You can generate a component with ember g component my-component
See also: Ember Components Tutorial
I hope this answers some of your questions.
Yeah, I get that, sadly. The difficulty that Iām having is understanding when to put things in the controller versus the component-file.js that is generated along with the component-file.hbs.
How is that determined? In the example posted here, it seems like both are being used.
I think in this transition period one should just look at a controller as a means to pass values to a component.
With that being said itās not going to be a train-smash if you just continue using controllers for the time being and migrate later once routable components are available. Use components for code that you would like to reuse across many different routes, or code that you would just like to consolidate into an easy to use wrapper.
So, Iāve been looking at your code. Lets see some of the things that really make it confusing for someone starting like me.
Iām trying to figure out this:
I have a component that is a form.
Iām submitting the form data to a function (in the component).
That function needs to make a RESTful call to look up an entry and load my model.
So, Iām looking through your code for the same. It seems like the āpostā object seems like a decent place to start.
Checking out your compoent for blog-post. The HBS is pretty straight forward. The JS, is well, confusing already.
And, nowhere in the code can I find a āsaveActionā referenced, or a ādeleteActionā anywhere. So, where do these go when they are clicked? Are they key words or something?
So, then, Iām trying to figure out how youāre using controllers. I check out your controller post.js.
https://github.com/broerse/ember-cli-blog/blob/master/app/controllers/post.js
Cool. Looks like a pretty straight forward pointer to where I can see these functions and how it gets the authors and their names, right? NOPE. I go and look into your controller posts.js
Uhm, what? Where is authors? You specifically wrote ācontrollers.posts.authorsā which, to my thought, should mean a function in the controllers.posts to get the authors. What is this even referencing?
I canāt see how you are actually tying your model data into your component here, because the references donāt seem to follow what they sayā¦
Any chance you can help me understand this?
I will try to find some time to answer this tomorrow. I think I can help you understand this. For starters saveAction
and deleteAction
are here:
https://github.com/broerse/ember-cli-blog/blob/master/app/templates/post.hbs
So let me try to explain the deleteAction
and the authors
on the controller. We are in the middle of switching to routable components. Therefore we still need to pass data like authors
from the route to a controller. You do this with setupController
. The default for Ember is content
. If you need more models like authors
you can use Ember.RSVP.hash
to get them and wait for all promises to resolve. See:
Now we can move authors
to the post controller.
https://github.com/broerse/ember-cli-blog/blob/master/app/controllers/post.js
Because controllers are singletons this is not repeated every time you switch form post to post.
Now 'actions`ā¦ If you call an Action on a controller and it is not handled there it will bubble to the route. If it is not handled by that route it will bubble up the three until it is handled. See:
-
/templates/post.hbs
deletePost
action can come from a component. -
/controllers/post.js
deletePost
not found. => Bubble -
/routes/post.js
deletePost
not found. => Bubble -
/routes/posts.js
deletePost
found. => Action
Now you know Bubble forget it. Actions in components donāt bubble. This is done because to bubble path above confuses a lot of people and it not always directly clear what is happening. Now lets follow the deletePost
from the component until it reaches the controller above.
-
/components/blog-post-edit.hbs Fire
deletePost
-
/components/blog-post-edit.js bubble yourself to
deleteAction
defined by the next component ācallā -
/components/blog-post.hbs your original
deletePost
action is now calleddeleteAction
but it is the same action bubbling. -
/components/blog-post.js bubble yourself to
deleteAction
defined by the next component ācallā - /templates/post.hbs we are on the controller explained above and automatic bubbling starts from here.
So lets hope routable components land soon in Ember and skip the controller and the automatic bubbling.
I hope this helps you understand this.
This helped immensely. Thank you so much.
No thanks.
Just a warning: the component actions I use now are almost the old 1.0 way. Closure Actions will be the way to go. I will move my example to Closure Actions when Ember-CLI uses Ember 2.0 by default.
Are you reading my mind?
I was just watching thisā¦