Table of Contents
How to create a new Ember project
Router and Router
Controller
Template
Components
Ember-Data
Generate user model
Generate user route
Set fixed data and generate user templates
Show individual users
Edit single user
Conclusion
What is the difference between Ember and Ember CLI?
How to install Ember CLI?
I get an error message saying "You must be inside the Ember CLI project to use the serve command". What does this mean?
How to create a new Ember application using the Ember CLI?
What basic Ember CLI commands should I know?
How to build my application for production using Ember CLI?
How to generate new files in my Ember application using Ember CLI?
How to start a development server using Ember CLI?
How to update the Ember CLI?
What is the purpose of the.ember-cli file?
Home Web Front-end JS Tutorial Getting started with Ember and Ember CLI

Getting started with Ember and Ember CLI

Feb 19, 2025 am 11:56 AM

Getting started with Ember and Ember CLI

Core points

  • Ember CLI is a command line tool built for Ember, which combines a variety of functions such as generator, compressor, CSS preprocessor compiler, automatic reloading and ES6 module loader. It can be used as an alternative to tools like Grunt and Gulp for setting up new Ember projects.
  • Ember follows the concept that conventions are better than configuration, which means it has many default settings that simplify the development process. Key elements include routing, controllers, templates, components, and Ember-Data.
  • This tutorial provides a step-by-step guide on how to build a simple contact manager application using the Ember CLI. It covers the capabilities of creating new Ember projects, setting fixed data, generating user models and routing, creating user templates, and adding display and editing user information.
  • Ember is a powerful JavaScript framework for building large web applications. With the Ember CLI, it provides a standardized development environment that makes it easier to manage dependencies, automate tasks, and execute best practices.

Ember has undergone many changes over the years. The biggest change is the introduction of Ember CLI, a command line tool built for Ember. It combines a variety of functions such as generator, compressor, CSS preprocessor compiler, automatic reloading and ES6 module loader. This command line tool will help you reduce the time you spend setting up tools like Grunt and Gulp. It's arguably a good alternative to these tools for any brand new Ember project. In this article, you will learn how to build a simple contact manager application using the Ember CLI. This tutorial is slightly different from other articles about Ember that I posted on SitePoint because they don't come with the Ember CLI. However, most of these concepts still apply, so I recommend you check them out and keep learning. The complete code for this article can be found on GitHub.

How to install Ember CLI

To install the Ember CLI, you need to install several dependencies first. The first one is Node.js. You need at least version 0.12.x. Next, you need to install Bower, which can be done by running the following command:

npm install -g bower
Copy after login
Copy after login
Copy after login
Copy after login

Then, to install the Ember CLI, run the following command:

npm install -g ember-cli
Copy after login
Copy after login
Copy after login
Copy after login

How to create a new Ember project

Before starting a wonderful operation, you need to open the terminal and execute the following commands in order to create a new project folder called contactmanager:

ember new contactmanager
Copy after login
Copy after login
Copy after login

Second step, go to the directory and install all npm and Bower dependencies using the following command:

cd contactmanager
npm install
bower install
Copy after login
Copy after login
Copy after login

At this time, start the built-in Ember server by executing the following command:

ember serve
Copy after login
Copy after login
Copy after login

You can now access your new application at URL localhost:4200. This is the default port for the Ember application running on the local computer, but you can change it as you like. If you follow all the steps instructed, you should now see a title in your browser that says "Welcome to Ember".

Ember Conventions and Structures

Before we dive into the application, let's review some of the Ember conventions.

Router and Router

Routing is the entry point for the Ember application. Router definitions are used in the app/router.js file. They allow you to access different parts of the application. For example, if you decide that you need to manage users in your application, you must define user routes. You can do this using the following syntax:

npm install -g bower
Copy after login
Copy after login
Copy after login
Copy after login

This will create the following URL for us:

  • /users/
  • /users/index/
  • /users/loading/

By convention, when you define a route, Ember expects to find other association types, such as routes, controllers, and templates. We can decide to create these types explicitly, or allow Ember to create them for us. In many applications, you will most likely have to create them yourself, but it's up to you. Remember, it is crucial to distinguish between routers and routes. The URL structure we created above is done using a router. These only show the intent we want to use these URLs in the application. We haven't created actual routes, but just URLs for those routes. To create a route we have to follow this process in the routes folder. If you are confused, don't worry, as I'll look at this topic more deeply later in this article.

Controller

Controller is a type used to store view state, located in the app/controllers folder. They work in conjunction with routing. In this case, the above URL corresponds to /user/ and requires a controller named /users/. Also here, we are free to choose whether to define it ourselves. The controller also defines event handlers for view operations (such as clicks, hovers, etc.).

Template

The template is the representation part of Ember. You write it in a template language called Handlebars, which compiles to pure HTML. The template is located in the app/templates folder.

Components

Components are small self-contained functional blocks. You can think of them as a combination of representations and features, they are reusable and easy to maintain.

Ember-Data

This is a library maintained by the Ember core team that complements the Ember core and acts as a front-end ORM for managing data models. There are other alternatives I haven't used before and are out of the scope of this article, as we'll be using Ember-data.

App

The contact management application we will build will include a list of users and their available contact information. The application will allow us to create, edit, delete and view users. To make our application concise and clear, we will use the fixed adapter that comes with the Ember CLI. This acts as a backend, except that the data is not persisted when the page is refreshed. First, if you haven't created it, use the ember new contactmanager to create a new Ember project.

Generate user model

Move to the project folder and generate the user model using the following command:

npm install -g ember-cli
Copy after login
Copy after login
Copy after login
Copy after login

This will create a file named user.js in app/models, with the following content:

npm install -g bower
Copy after login
Copy after login
Copy after login
Copy after login

Make the necessary changes so that the export statement looks like this:

npm install -g ember-cli
Copy after login
Copy after login
Copy after login
Copy after login

This defines the properties our user model will have.

Generate user route

Now, add the following lines to your router.js file to give us some available URLs:

ember new contactmanager
Copy after login
Copy after login
Copy after login

We have three new URLs. One is to list users, the other is to view individual users, and the last is to edit user information. Next, let's create a user route using the following command:

cd contactmanager
npm install
bower install
Copy after login
Copy after login
Copy after login

This route will be used to retrieve our user list. Change its content using the following code snippet:

ember serve
Copy after login
Copy after login
Copy after login

Set fixed data and generate user templates

At this point, let's add some temporary data to our application. To do this, run the following command:

Router.map(function() {
  this.resource('users', function() {});
});
Copy after login

This will generate a file named application.js in the app/adapters/ folder. By default, Ember uses RestAdapter to query the model. This adapter assumes that you have a backend system that provides JSON data to your Ember client application. Since we don't have a backend, in this case we want to use fixed data instead. Therefore, we will update the adapter code as follows:

ember generate model user
Copy after login

and add the following to your user model to create some fixtures.

import DS from 'ember-data';

export default DS.Model.extend({
});
Copy after login

If you navigate to URL localhost:4200/users, you will only see the old greeting messages, and not the user pinned data we just added. To view user data, we need to create a template for the user using the following command:

export default DS.Model.extend({
  firstName: DS.attr(),
  lastName: DS.attr(),
  addressLine: DS.attr(),
  postCode: DS.attr(),
  country: DS.attr()
});
Copy after login

This creates a file named users.hbs in the app/templates/ folder. Open this file and update its contents as follows:

Router.map(function() {
  this.resource('users', function() {
    this.route('show',{path: '/:user_id'});
    this.route('edit',{path: '/:user_id/edit'});
  });
});
Copy after login

You should now see a list of users, each with an edit text next to it. Because we only have one user in the fixed data, we will only see one user. You can add more user objects to the user fixture as needed. Just make sure each object has a unique ID.

Show individual users

Now that we have listed our users, let's see how to display the full information of the user. first. Change the code in the users template by enclosing the "edit" text next to each username with a link. Then, change "Edit" to:

ember generate route users
Copy after login

Next, let's generate a user controller using the following command:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.find('user');
  }
});
Copy after login

Inside (user controller), change the content to the following:

ember generate adapter application
Copy after login

After finishing, use the following command to generate a template for editing the user:

import DS from 'ember-data';

export default DS.FixtureAdapter.extend({
});
Copy after login

Currently, the created template (app/templates/users/show.hbs) is empty. Open it and add the following code:

User.reopenClass({
   FIXTURES: [{
      id: 1,
      firstName: 'James',
      lastName: 'Rice',
      addressLine: '66 Belvue Road',
      postCode: 'M235PS',
      country: 'United Kingdom'
   }]
});
Copy after login

Do this and you should be able to see the full information for each user you click on.

Edit single user

If you want to edit a single user, you have to follow some simple steps. First, link to user edit routes by enclosing the "edit" text next to each username with a link. Then, change "Edit" to:

npm install -g bower
Copy after login
Copy after login
Copy after login
Copy after login

Next, let's generate a user controller using the following command:

npm install -g ember-cli
Copy after login
Copy after login
Copy after login
Copy after login

Inside (user controller), change the content to the following:

ember new contactmanager
Copy after login
Copy after login
Copy after login

After finishing, use the following command to generate a template for editing the user:

cd contactmanager
npm install
bower install
Copy after login
Copy after login
Copy after login

In the new template app/templates/users/edit, paste the following code:

ember serve
Copy after login
Copy after login
Copy after login

This code calls the saveUser() function on our controller when submitting the form. This function passes the user being edited and saves the modified information. With this change, you can edit the details of the user when you click on the edit link for the user. When you click the Save button, you can save them, after which you will be redirected back to the user list. Long live! We now have a simple contact list manager. You can convert it into a full application by connecting it to a real backend to persist data when the page refreshes. I also encourage you to add delete functionality to the app so that you can delete unwanted users at any time.

Conclusion

Emberhttps://www.php.cn/link/0e0f9e664029e8912996d65c1cf09761 is a framework for building large web applications. It has the idea that conventions are better than configuration, which means it is based on several common decisions and has many defaults (conventions), which makes your development process easier. This way, you don't have to make many trivial decisions during the development process. I hope you enjoyed reading this tutorial and learning new things about how to use such a powerful and simple JavaScript framework in your project. Please let us know what you think in the comments below. You can find the code for the application on GitHub.

Frequently Asked Questions about Getting Started with Ember and Ember CLI

What is the difference between Ember and Ember CLI?

Ember is a JavaScript framework for building web applications, and Ember CLI is a command line tool that helps you create, develop, and build Ember applications. Ember CLI provides a standardized development environment that makes it easier to manage dependencies, automate tasks, and execute best practices.

How to install Ember CLI?

To install Ember CLI, you need to install Node.js and npm on your system. After installing these prerequisites, you can install the Ember CLI using the following command in the terminal: npm install -g ember-cli.

I get an error message saying "You must be inside the Ember CLI project to use the serve command". What does this mean?

This error occurs when you try to run the ember serve command outside the Ember CLI project directory. To resolve this issue, use the ember serve command to navigate to the root directory of the project before running cd.

How to create a new Ember application using the Ember CLI?

You can use the ember new command followed by the name of the application to create a new Ember application. For example, ember new my-app will create a new Ember application called "my-app".

What basic Ember CLI commands should I know?

Some basic Ember CLI commands you should know include ember new (create a new application), ember serve (start a development server), ember generate (generate a new file), and ember build (build your application program for deployment).

How to build my application for production using Ember CLI?

You can use the ember build command and set the --environment option to "production" to build your application for production. The command looks like this: ember build --environment production.

How to generate new files in my Ember application using Ember CLI?

You can use the ember generate command followed by the file type and its name to generate a new file in the Ember application. For example, ember generate route about will generate a new route called "about".

How to start a development server using Ember CLI?

You can start the development server using the ember serve command. This will start the server and make your application accessible at http://localhost:4200.

How to update the Ember CLI?

You can use the command npm uninstall -g ember-cli to uninstall the old version and then use the command npm install -g ember-cli to install the new version to update the Ember CLI.

What is the purpose of the.ember-cli file?

.ember-cli File is the configuration file of the Ember CLI. It allows you to customize the behavior of your Ember CLI project. For example, you can specify the default port of the development server, enable or disable certain features, and more.

The above is the detailed content of Getting started with Ember and Ember CLI. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

See all articles