From Gulp.js to ember-cli
a how & why
Full disclaimer: I am not pretending to be an expert, so I am open to your input. Be nice ☺
Today started out frustrating. I reviewed some code several times that I had written and it looked correct. I kept getting the same errors over and over again. Despite my best efforts, I couldn’t even get my Ember app to render. I decided to close everything, take a break and revisit this later. Right as I was closing all of my windows and tabs, I realized something infuriating — I had mistyped something in a handlebars template earlier and gulp just quit. There was no warning. Just a silent error. Why wouldn’t it just keep trying to compile until I fixed the error? This was not the first time this had happened.
My first intuition was just to find some plugin (thanks @JonKoops) that would fix this problem as I’m sure I am not the first this has happened to. However, I had just watched a video of Stefan Penner on ember-cli and it looked promising. It looked like it solved the problem mentioned above and also it looked like it would address a few more of my fears of switching away from gulp.
I was apprehensive to switch to ember-cli. Truthfully, I had a build solution that worked, it just didn’t work well. And ember-cli is still considered a work in progress. What if I spend a bunch of time switching code to only end up in a similar situation? At least I am familiar with the the situation I am in now. Also, ember-cli was built on top of broccoli. I know many people are in love with broccoli, but would it really be worth learning yet another build tool? And I know es6 is cool, but my project doesn’t implement it at all. I am not that familiar with it in the first place. How hard would it be to convert all of my old code to use it?
I decided to only give myself the first half of the day to try to convert my project from a custom gulpjs build to an ember-cli build. If I could not get it done, I would just try using it on some future project.
In less than 4 hours I had completely replicated the project with ember-cli. And it worked.
Am I happy I switched to using ember-cli? Was it worth it? Absolutely. Here’s why:
Installation is a breeze
npm install -g ember-cliember new projectName
cd projectName
ember server
done. With just a few commands, My project had been scaffolded, any dependencies had been brought in with bower, those dependencies as well as your source code were compiled with broccoli, a server was started and it has autoload built in. As a bonus, all of your code is scrubbed with jshint (which quickly reminded me that I don’t like to use semicolons). I do not know if it gets any easier than that.
ES6 and the Ember Resolver are awesome
Like I mentioned earlier, I was scared to dive into the foreign world of ES6. What is a transpiler anyway? But if you take the time to read through the ember-cli, you will realize that with the help of the Ember Resolver, the changes you will have to make are pretty common sense. Plus the generators built into ember-cli give you a pretty good starting point. Let’s walk through a simple example.
Get Cully Mason’s stories in your inbox
Join Medium for free to get updates from this writer.
Say you wanted to create a controller for the comments section for a given post for your app. So you type:
ember generate controller post/commentsThis messed me up when I first started. Notice how you don’t type ‘ember generate postComments’ but post/comments. Anything that is nested in your app will be nested in the command.
It will ask you whether you want to create an array controller, object controller or neither. Once you make your choice, under the controllers folder, ember-cli will create a folder named post and a file named comments.js. If you open up the comments.js file, you will see the following:
var PostCommentsController = Ember.ObjectController.extend({});export default PostCommentsController;
Which isn’t much different than what I am used to:
App.PostCommentsController = Ember.ObjectController.extend({});
Once I realized how similar the code was, a majority of my move to ember-cli was just a copy and paste job with a few caveats:
- The naming convention is a bit different than what I am used to. If something in your app is multiple words, name the file with hyphens, and inside the app use camelCase. Also, all filenames are lowercase. So if you want to create a route called “My Awesome Site”, the file would be called my-awesome-site.js and you would export MyAwesomeSiteRoute.
- Because you aren’t putting everything in the global App namespace, things like mixins are handled a little differently. Say you wanted to write a mixin to ensure that the comments from the controller above are filterable. So you would type ember generate mixin filterable. Of course under the mixins/filterable.js you get this:
var filterable = Ember.Mixin.create({});
export default filterable;To use it, you need to import it into your controller file. So it looks like this:
import filterable from “appName/mixins/filterable”;var PostCommentsController = Ember.ObjectController.extend(filterable,{
});
export default PostCommentsController;
If you look at the docs, there are a few ways to import your mixins, helpers and such. I found what made the most sense was to base your path on what you called your app.
Note: If you forgot what you called your app, you can see under your index.html file in the statement window.projectName = require(‘project-name/app’)[‘default’].create(ENV.APP);
When your controller has needs
If you have a controller that depends on another controller, you typically type:
needs: [‘postComments’]As stated in the docs, because of the way files are nested using ember-cli, you would need to make a few minor changes:
needs: [“post/comments”],
postComments: Ember.computed.alias(‘controllers.post/comments’)and whenever you need it, you can use it like any other property:
var commentsController = this.get(‘postComments’);What about ember-data?
When you create a new project, the beta for ember-data is included and using it is well documented.
Creating Models
The ember-cli generator does not include models but it is straight forward to create yourself. If you wanted to create a Post model, you would create a post.js file in the models folder that looks like this
export default DS.Model.extend({
// attributes go here
});With only that code, how does Ember know that it is a Model for a Post? The Ember resolver is smart enough to know that since you placed post.js in the Models folder, that it must be a Model named Post. In fact, this works for other parts of your app like your Controller and Route files.
Adapters and Serializers
Even though ember-cli didn’t generate folders for adapters or serializers, you can create them yourself. An adapter for your Post model would reside in app/adapters/post.js. Application wide adapters and serializers will be contained in the their appropriate folders in a file named application.js.
Fixtures
Because my project had previously been in production, I didn’t need to use the fixture adapter, but I have previously. I would say this is probably where the documentation is a bit lacking (or I probably just missed it) but nonetheless here is how to get up and running with fixtures:
- I created an application.js in the app/adapters folder that contains a single line: export default DS.FixtureAdapter.extend();
- Lets say you need fixtures for A Task Model. Under your app/models directory, you would create a file called task.js that looks like this:
var Task = DS.Model.extend({
title: DS.attr(‘string’),
description: DS.attr(‘string’),
});Task.reopenClass({
FIXTURES: [
{ id: 1, title: ‘Title A’, description: ‘Body A’, owner: 1 },
{ id: 2, title: ‘Title B’, description: ‘Body B’, owner: 2 } ]
});export default Task;
After that, you are up and running with fixtures.
Bower is your friend again
I think bower, in concept, is awesome. I always start out using it. But unfortunately, as my project becomes larger I tend to forget about it. Because you don’t use globals with ember-cli I was a bit worried how using outside scripts would work. As it turns out, it is very straight forward. In my current project I use a tool that reads excel files. In order to use it, I use bower to pull in the dependency. Then in my Brocfile, tell broccoli where it can be found like so:
app.import(‘vendor/js-xls/xls.js’);And that was it.
Using Ember.js Beta
Ember-cli only pulled in 1.5.1. No problem. I just opened up my bower.json and changed the ember line to:
“ember”: “1.6.0-beta.3"and it just worked.
Done?
Once I was done converting my code I simply typed ember build and all of my code was compiled and minified into a dist directory.
Theres more..
But, this is getting a bit long. Really switching to ember-cli from a tool like grunt or gulp is very painless. The biggest “hurdle” if you want to call it that, was naming everything appropriately and in true Ember.js style, the error messages are extremely helpful. The best part is that through the migration to ember-cli, I ended up with code that was better organized and cleaner. Also, the build times are insanely fast.
Even better, after I got done converting my project, I had an idea for a component that I never really hammered out because I didn’t want to go through the hassle of setting up a new project for just a component. With ember-cli, that was just a generator away.
The road ahead
There are a few things that I didn’t fully get to try or figure out but will investigate in the future:
- Testing. I know this is bad, but I never really fooled with it on my ember apps because it took so much time to set up. At first glance, ember-cli appears to offer a very approachable way to get into testing. Several of the generators also generate a test file.
- SASS/CSS/Bootstrap I just ran out of time. I know ember-cli can do it, but for now I just included bootstrap inside my app/styles folder.
Hopefully this will help others make the same migration I have. I cannot see myself going back to anything else. The pace at which ember-cli is improving is insane and is only getting better. If you have any questions, please feel free to reach out to me on Twitter. I am by no means an expert, but I will do my best. As always Ember has an excelent support community and there is a very useful #ember-cli channel on freenode.
Cheers.







