Ember.js is a modern front-end JavaScript framework that focuses on conventions and over configurations, and has attracted much attention recently. This article will introduce the key concepts of the framework and its implementation capabilities by constructing a simple dice roll application.
Our sample application is a dice roll program that allows users to roll dice and view history. The full code for the app is available on Github.
Ember.js integrates many modern JavaScript concepts and technologies, including but not limited to:
To use Ember.js, you need to install the latest versions of Node.js and npm. If not, you can download and install it from the official website of Node.js.
To be noted, Ember.js is purely a front-end framework. It has multiple ways to interact with the backend of your choice, but the backend itself is not handled by Ember.js.
Ember.js' power comes from its command line interface (CLI). This tool—called ember-cli—drives most of the development lifecycle of Ember.js applications, from creating applications, adding features, to running test suites and launching actual projects in development mode.
When developing an Ember.js application, almost everything will involve this tool to some extent, so it is important to understand how to best use it. We will use it in this article.
First of all, we need to make sure that the Ember.js CLI is installed correctly and updated. This can be done by installing from npm as follows:
$ npm install -g ember-cli
We can check if it has been installed successfully by running the following command:
$ ember --version ember-cli: 2.15.0-beta.1 node: 8.2.1 os: darwin x64
After installing ember-cli, you can start creating applications. This is where we will use the Ember.js CLI tool for the first time – it creates the entire application structure, sets everything up for it to run.
$ ember new dice-roller ... (安装过程输出) ...
This will create a complete application that can be run immediately. It even sets up Git as source control to track your work.
Note: You can disable Git integration if you want, or you can choose to use Yarn instead of npm. This and more are described in the tool's help documentation.
Now, let's see what it looks like. Use ember-cli to start the Ember application for development:
$ npm install -g ember-cli
Now we are ready. The application is running on https://www.php.cn/link/988aaaa9bd19c1e0f8043a132bbf1af3 as follows:
It also runs a LiveReload service that automatically monitors file system changes. This means that you can have very fast turnaround times when tweaking your website design.
Let's try it?
The initial page already tells us what to do, so let's change the main page and see what happens. We will change the app/templates/application.hbs
file to look like this:
$ ember --version ember-cli: 2.15.0-beta.1 node: 8.2.1 os: darwin x64
Note: The
{{outlet}}
tag is part of how Ember routing works. We'll cover it later.
The first thing to note is the output of ember-cli, which should look like this:
$ ember new dice-roller ... (安装过程输出) ...
This tells us it has found that we have changed the template and rebuilt and restarted everything. We have no participation in this part of the content.
Let's look at the browser now. If you have LiveReload installed and run, you don't even need to refresh your browser to pick it up, otherwise you need to reload the current page.
While it wasn't very interesting, we achieved this without putting any effort.
In addition, we also got a fully set test suite that works. As expected, this is also run using the Ember tool:
$ cd dice-roller $ ember serve ... (启动过程输出) ...
Note that PhantomJS is mentioned in the output. This is because integration tests running in the browser are fully supported, which by default run headless in the PhantomJS browser. If you prefer, you can fully support running them in other browsers, and when setting up Continuous Integration (CI), it is worth doing so to ensure your application works properly in all supported browsers.
...(The remaining content is similar to the original text. Some sentences can be adjusted as needed to keep the overall meaning unchanged, and the image format and position are preserved)
The above is the detailed content of Ember.js: The Perfect Framework for Web Applications. For more information, please follow other related articles on the PHP Chinese website!