This post is also on my blog, since Gist doesn't support @ notifications.
Components are taking center stage in Ember 2.0. Here are some things you can do today to make the transition as smooth as possible:
-
Use Ember CLI
-
In general, replace views + controllers with components
-
Only use controllers at the top-level for receiving data from the route, and use
Ember.Controller
instead ofEmber.ArrayController
orEmber.ObjectController
-
Fetch data in your route, and set it as normal properties on your top-level controller. Export an
Ember.Controller
, otherwise a proxy will be generated. You can use Ember.RSVP.hash to simulate setting normal props on your controller.```js //controllers/index.js import Ember from 'ember'; export default Ember.Controller; //routes/index.js model: function() { return Ember.RSVP.hash({ users: this.store.find('user') }); }, setupController: function(controller, models) { controller.setProperties(models); } ``` Now you can just refer to the keys in your route's template: ```hbs //templates/index.js <h1>Users</h1> {{user-list users=users}} ``` This controller/template pair will eventually become a routable component.
-
In your templates, stay away from things like
ItemController
s and calls torender()
. Use components instead. -
Don't use views
-
Write your app in the "data down, actions up" paradigm
- Not currently enforced, but you can still structure your app this way
- Stay away from two-way bindings and mutability
-
Don't use
each
orwith
in the context-switching form. That is, use{{#each users as |user|}} {{user.firstName}} {{/each}}
instead of
{{#each users}} {{firstName}} {{/each}}
-
Use pods
-
Use
this.route
instead ofthis.resource
inRouter.map
. Couple this with pods and you'll quickly understand why you don't want to useresource
anymore - your directory structure inpods
will now mirror the view hierarchy of your UI.
Better apps
Follow these tips, and your apps will be ready for Ember 2.0. You'll also learn a lot about writing apps that are better structured and easier to understand!
Deprecations will be coming to help you move towards these newer practices. The goal is, if your app runs on the final version of 1.x with no deprecations, it should be able to run in 2.0.
Dealing with the 'loading' issue right now. We have the set up outlined above. We have an index page that has 4 components that get assigned from 4 models we load through the use of Ember.RSVP. The issue is we have to wait to load all the 4 models. And using the 'loading' template, we can show a loading screen for the whole page. What I would like to do is show the loading pages independently for each component. So we would load the componens and show 'loading' within them.
One solution I can think of is to inject 'store' into the components and treat them like 'routable components' and assign them each a model (which gets passed down as a string -model name-) Then components can load their models using the model name sent down from the Router -> Template and use a component for the loading spinner display.
@samselikoff you have been guiding me through your past posts on my attempt to fully implement our current site with Ember 2.0 way. Can you shine some light into this discussion?