Working with Ember CLI

Ember CLI is an integrated, rapid development environment for Ember applications. Based on Broccoli, a fast and reliable asset pipeline that runs on Node.js, Ember CLI is a powerful command-line interface that integrates many productivity tools and optimization utilities necessary for JavaScript development.

Ember CLI provides the following features and tools for Ember development:

  • It creates a strong, convention-based project structure for Ember applications
  • It generates Ember-specific application resources, such as routes, templates, and components, from the command line
  • It supports template authoring in the Handlebars, HTMLBars, and Emblem.js formats
  • It supports scripting in ES2015 (ES6) modules, CoffeeScript, and EmberScript syntaxes
  • It supports CSS authoring in CSS, Sass, Compass, and Stylus
  • It converts Node.js-style ES2015 modules into RequireJS-model AMD modules
  • It integrates the npm and Bower package managers for managing dependencies to JS libraries
  • It integrates a development server with LiveReload, which automatically rebuilds and updates code changes to all connected browsers
  • It performs asset management functions for application resources (combining, minifying, uglifying, versioning, and so on)
  • It enables the sharing of code and functionality using add-ons and blueprints

Later in this chapter, we will use Ember CLI as a development tool for building an Ember application and its various artifacts.

Setting up Ember CLI

Ember CLI depends on Node.js. So, the first step is installing Node.js. Follow the instructions given on the website http://nodejs.org to set up Node.js.

Once Node.js is installed, you can install Ember CLI using npm, with the following command:

npm install -g ember-cli

Now, install Bower using the following command:

npm install -g bower

You may optionally install Watchman for better watching of code changes and the PhantomJS test-running environment.

Getting started with Ember CLI commands

Once Ember CLI is installed, you may start creating Ember applications incrementally using this set of commands to generate the required Ember artifacts:

Command

Purpose

ember

Prints the available commands.

ember new <appname>

Generates a fresh new project root folder with the same name as <appname>, the whole project structure, and all the necessary artifacts for a starter Ember application.

ember init

Turns the current directory into an Ember application and generates all necessary artifacts.

ember build

Builds and generates the deployable to the dist directory. Specify the environment using the environment flag, which defaults to development.

ember server (or serve)

Starts the development server at port 4200. You may point to another port using the --port flag, for example, ember serve --port 8080.

ember generate <generatortype> <name> <options>

Generates specific generators, such as route, template, and helper, with the given name and options. Type ember help generate for the full list of available generators. Use the --pod flag for generators in the POD structure (explained later).

ember destroy <generatortype> <name> <options>

Removes artifacts created using the ember generate command. Remember to use the --pod flag if it was used while generating the artifact.

ember test

Runs tests written in the application using the Testem test runner.

ember install <addon-name>

Installs the given add-on into the application and registers it in the package.json file.

The Ember project structure

When you use the ember new <project-name> command, Ember CLI generates and organizes files in a specific structure based on convention and then compiles them and performs a set of tasks during building and runtime. The following table describes the folder layout and important files generated by Ember CLI:

File/Folder

Description

app/

This is the Ember application root. The index.html file and all your JavaScript files and templates go inside this, under proper subdirectories. Everything except index.html is compiled through the ES6 module transpiler, minified and concatenated to <app-name>.js, and then loaded by the index.html file at build time.

app/index.html

This is the only HTML page loaded from the server, which boots the Ember application on load from <app-name>.js, and is loaded using the <script/> tag embedded in it. Ember builds the entire DOM structure from inside this foundation HTML document in the browser at runtime.

app/app.js

This is the Ember application module. This is the application's entry point, where all the other modules are initialized and injected in order to create the entire application instance based on the resolver and environment-specific configuration.

app/router.js

This is the router configuration module of the application.

app/adapters/

Adapters for Ember Data modules go here. This folder is generated when the ember generate adapter <model-name> command is executed for the first time.

app/components/

All components go here, unless the --pod option is used.

app/controllers/

All controllers go here, unless the --pod option is used.

app/helpers/

All helpers go here, unless the --pod option is used.

app/models/

All models go here, unless the --pod option is used.

app/routes/

All routes go here, unless the --pod option is used.

app/services

All services go here, unless the --pod option is used.

app/styles/

Put all your style sheets for the application, whether Sass, LESS, Stylus, Compass, or plain CSS, here. Only plain CSS is supported by default; you can enable other types by installing the appropriate npm modules. For Sass, type ember install ember-cli-sass in the command line. For LESS, the command is ember-cli-less; for Compass, ember-cli-compass-compiler, and so on. For the default CSS option, add your styles to app.css. You can also organize the styles in different CSS files and import them to your app.css file.

app/templates/

All templates go here, unless the --pod option is used.

bower.json

This is the Bower configuration file.

bower_components/

Dependencies managed by Bower go here.

config/

Application configuration files fall here.

config/environment.js

Your environment-specific configurations go inside this file.

dist/

The deployable files generated by the build process go here. This is what you need to distribute for release.

ember-cli-build.js

This is the Broccoli build file. Include all resources managed by Bower and npm here.

node_modules

All node dependencies managed by npm go here.

package.json

This is the NPM dependency configuration file.

public/

This is a directory for uncompiled assets, such as fonts and images. The contents are copied as they are.

server/

This is where you can set up a development server for mock APIs and tests.

tests/

All your unit and integration tests go here.

tmp/

This is a temporary folder for build execution.

vendor/

Place your external dependencies that are not managed by npm or Bower here.

At the end of the build process, Ember CLI generates the deployable at dist/directory. You need to distribute the contents of this directory for hosting the deployable on a web server on release.

Working with the POD structure

By default, the ember generate <generator> command generates artifacts inside specific resource directories directly under the app root directory. So, all your routes go under app/routes, templates under app/templates, and so on. However, this becomes a bit unmaintainable as the application grows. To solve this problem, Ember CLI provides the option of organizing your files in a feature-driven (POD) structure using the --pod flag when you generate an artifact using the ember generate command.

In order for the POD structure to work, you need to first configure the POD directory in config/environment.js as given in the following code:

module.exports = function(environment) {
  var ENV = {
    ...
    podModulePrefix: 'my-ember-app/pod-modules',
    ...
    },
    ...
  return ENV;
};

The preceding snippet specifies that all the artifacts you generate with the --pod flag will be generated inside the <app-root>/pod-modules directory.

Once you configure the POD, you can start generating your artifacts with the --pod flag.

For example, if you want to generate a route inside the POD structure, use the following command:

ember generate route user --pod

This will generate the route file at /app/pod-modules/user/route.js.

POD modules group all the artifacts related to a feature in one place, thus making it more manageable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset