Ember CLI ships with “Blueprints”, snippet generators for many of the entities - models, controllers, components, and so on - that you’ll need in your app. Blueprints allow us to share common Ember patterns in the community and you can even define your own.
To see a list of all available blueprints, with a short description of what they do, run ember generate --help or ember g --help, for short, at any time. For a longer, more detailed description of each blueprint, look in the appendix to this guide.
This in an example of how to generate a Route Blueprint.
ember generate route foo
installing
create app/routes/foo.js
create app/templates/foo.hbs
installing
create tests/unit/routes/foo-test.jsFor a list of all available blueprints, run:
ember help generateYou can define your own blueprints using ember generate blueprint <name>:
ember generate blueprint foo
installing
create blueprints/.jshintrc
create blueprints/foo/files/.gitkeep
create blueprints/foo/index.jsBlueprints in your project’s directory take precedence over those packaged with ember-cli. This makes it easy to override the built-in blueprints just by generating one with the same name.
You can generate certain built-in blueprints with a pods structure by passing the --pod option.
ember generate route foo --pod
installing
create app/foo/route.js
create app/foo/template.hbs
installing
create tests/unit/foo/route-test.jsIf you have podModulePrefix defined in your environment, your generated pod path will be automatically prefixed with it.
// podModulePrefix: app/pods
ember generate route foo --pod
installing
create app/pods/foo/route.js
create app/pods/foo/template.hbs
installing
create tests/unit/pods/foo/route-test.jsThe built-in blueprints that support pods structure are:
Blueprints that don’t support pods structure will simply ignore the --pod option and use the default structure.
If you would like to use the pods structure as the default for your project, you can set usePodsByDefault in your environment config
to true. When usePodsByDefault is true, the --pod flag is essentially inverted. To generate or destroy a blueprint in the default
type structure while usePodsByDefault is true, use the --pod flag.
With the usePodsByDefault set to true.
1 // config/environment.js
2 module.exports = function(environment) {
3 var ENV = {
4 modulePrefix: 'my-new-app',
5 podModulePrefix: 'my-new-app/pods'
6 usePodsByDefault: true,
7 environment: environment,
8 baseURL: '/',
9 locationType: 'auto',
10 //...The following would occur when generating a route:
ember generate route taco
installing
create app/taco/route.js
create app/taco/template.hbs
installing
create tests/unit/taco/route-test.js
ember generate route taco --pod
installing
create app/routes/taco.js
create app/templates/taco.hbs
installing
create tests/unit/routes/taco-test.jsBlueprints follow a simple structure. Let’s take the built-in
helper blueprint as an example:
blueprints/helper
├── files
│ ├── app
│ │ └── helpers
│ │ └── __name__.js
└── index.jsThe accompanying test is in another blueprint. Because it has the same name with a -test suffix,
it is generated automatically with the helper blueprint in this case.
blueprints/helper-test
├── files
│ └── tests
│ └── unit
│ └── helpers
│ └── __name__-test.js
└── index.jsBlueprints that support pods structure look a little different. Let’s take the built-in
controller blueprint as an example:
blueprints/controller
├── files
│ ├── app
│ │ └── __path__
│ │ └── __name__.js
└── index.js
blueprints/controller-test
├── files
│ └── tests
│ └── unit
│ └── __path__
│ └── __test__.js
└── index.jsfiles contains templates for the all the files to be
installed into the target directory.
The __name__ token is subtituted with the dasherized
entity name at install time. For example, when the user
invokes ember generate controller foo then __name__ becomes
foo. When the --pod flag is used, for example ember
generate controller foo --pod then __name__ becomes
controller.
The __path__ token is substituted with the blueprint
name at install time. For example, when the user invokes
ember generate controller foo then __path__ becomes
controller. When the --pod flag is used, for example
ember generate controller foo --pod then __path__
becomes foo (or <podModulePrefix>/foo if the
podModulePrefix is defined). This token is primarily for
pod support, and is only necessary if the blueprint can be
used in pod structure. If the blueprint does not require pod
support, simply use the blueprint name instead of the
__path__ token.
The __test__ token is substituted with the dasherized
entity name and appended with -test at install time.
This token is primarily for pod support and only necessary
if the blueprint requires support for a pod structure. If
the blueprint does not require pod support, simply use the
__name__ token instead.
Variables can be inserted into templates with
<%= someVariableName %>.
For example, the built-in util blueprint
files/app/utils/__name__.js looks like this:
1 export default function <%= camelizedModuleName %>() {
2 return true;
3 }<%= camelizedModuleName %> is replaced with the real
value at install time.
The following template variables are provided by default:
dasherizedPackageNameclassifiedPackageNamedasherizedModuleNameclassifiedModuleNamecamelizedModuleNamepackageName is the project name as found in the project’s
package.json.
moduleName is the name of the entity being generated.
The mechanism for providing custom template variables is described below.
Custom installation and uninstallation behaviour can be added
by overriding the hooks documented below. index.js should
export a plain object, which will extend the prototype of the
Blueprint class. If needed, the original Blueprint prototype
can be accessed through the _super property.
1 module.exports = {
2 locals: function(options) {
3 // Return custom template variables here.
4 return {};
5 },
6
7 normalizeEntityName: function(entityName) {
8 // Normalize and validate entity name here.
9 return entityName;
10 },
11
12 fileMapTokens: function(options) {
13 // Return custom tokens to be replaced in your files
14 return {
15 __token__: function(options){
16 // logic to determine value goes here
17 return 'value';
18 }
19 }
20 },
21
22 beforeInstall: function(options) {},
23 afterInstall: function(options) {},
24 beforeUninstall: function(options) {},
25 afterUninstall: function(options) {}
26
27 };As shown above, the following hooks are available to blueprint authors:
localsnormalizeEntityNamefileMapTokensbeforeInstallafterInstallbeforeUninstallafterUninstallUse locals to add custom tempate variables. The method
receives one argument: options. Options is an object
containing general and entity-specific options.
When the following is called on the command line:
ember generate controller foo --type=array --dry-runThe object passed to locals looks like this:
1 {
2 entity: {
3 name: 'foo',
4 options: {
5 type: 'array'
6 }
7 },
8 dryRun: true
9 }This hook must return an object. It will be merged with the aforementioned default locals.
Use the normalizeEntityName hook to add custom normalization and
validation of the provided entity name. The default hook does not
make any changes to the entity name, but makes sure an entity name
is present and that it doesn’t have a trailing slash.
This hook receives the entity name as its first argument. The string returned by this hook will be used as the new entity name.
Use fileMapTokens to add custom fileMap tokens for use
in the mapFile method. The hook must return an object in the
following pattern:
1 {
2 __token__: function(options){
3 // logic to determine value goes here
4 return 'value';
5 }
6 }It will be merged with the default fileMapTokens, and can be used
to override any of the default tokens.
Tokens are used in the files folder (see files), and get replaced with
values when the mapFile method is called.
Called before any of the template files are processed and receives
the same arguments as locals. Typically used for validating any
additional command line options. As an example, the controller
blueprint validates its --type option in this hook.
The afterInstall and afterUninstall hooks receives the same
arguments as locals. Use it to perform any custom work after the
files are processed. For example, the built-in route blueprint
uses these hooks to add and remove relevant route declarations in
app/router.js.
If you don’t want your blueprint to install the contents of
files you can override the install method. It receives the
same options object described above and must return a promise.
See the built-in resource blueprint for an example of this.
ember generate acceptance-test signupember generate adapter application will generate an adapter called ‘ApplicationAdapter’ based off the DS.RESTAdapter by default.ember generate adapter user will in turn call this adapter UserAdapter and inherit from the Application Adapter unless you specify a base class.ember generate adapter-test applicationember generate addon awesome-addonember new.ember generate blueprint example-blueprintember generate component nav-barember generate component-test nav-barember generate controller users --type arrayember generate helper capitalizeember generate helper-test capitalizeember generate http-mock usersember generate http-proxyember generate in-repo-addon calendarember generate initializer current-userember generate initializer-test current-userember generate mixin filterableember generate mixin-test filterableember generate model userember generate model-test userember generate resource userember generate route userember generate serializer applicationember generate serializer-test applicationember generate service geolocationember generate service-test geolocationember generate template userember generate transform fooember generate transform-test fooember generate util fooember generate util-test fooember generate view userember generate view-test user