- Ember.js Cookbook
- Erik Hanchett
- 837字
- 2021-07-16 12:58:03
Defining an application template
To work with templates, we need to understand the basics on how properties bind with controllers and components. Here are a few recipes that go over how to accomplish this.
Getting ready
Before we get started, we'll need to generate a template.
- We'll first create a new application using Ember CLI:
$ ember new HelloWorldApp $ cd HelloWorldApp
This command will generate a new application that we can use for this recipe.
- Next, create a new route that will add a new template:
$ ember g route helloworld
This command will generate the template and routes file as well as unit tests. The template file is called
helloworld.hbs
and will be generated in theapp/templates
folder. The route file is calledhelloworld.js
and is located in theapp/routes
folder. Theroute.js
file will also get modified with the newhelloworld
route. We'll discuss more about routes in Chapter 4, Ember Router. - After this, we'll need to generate a
controller
:$ ember g controller helloworld
This will generate a new file called
helloworld.js
in theapp/controller
folder and a unit test intests/unit/controllers
. We are now ready to continue.
How to do it...
Let's take a look at adding properties to our new template file.
- Begin by editing the
helloworld.hbs
file. For this simple example, we'll create a string with the first and last name properties as follows:// app/templates/helloworld.hbs Hello World! My name is {{firstName}} {{lastName}}. {{outlet}}
Handlebar expressions are surrounded by double curly braces
{{ }}
and backed by a context. A context is an object from which Handlebar expressions read their properties. In this example, the context is the controller. The{{outlet}}
will render the template of any nested routes, which will be discussed in more detail later. - The controller will need to have the
firstName
andlastName
properties so that they can be displayed in the template:// app/controllers/helloworld.js import Ember from 'ember'; export default Ember.Controller.extend({ firstName: 'Erik', lastName: 'Hanchett' });
The controller has the same name as the template. The template, by convention, will retrieve the properties from the controller of the same name. Each of them is bound to each other. If any changes occur to the data, the other values will change.
Using templates with components
Similar to controllers, we can create a component that can act as a context for the template. In the component, we can set up properties that can be accessed by the template later.
- To create a new component, use the
generate component
command:$ ember g component hello-world
All components must have a dash in their names. This command will create the
hello-world.js
file in theapp/components/hello-world.js
folder, a template file in theapp/components/hello-world.hbs
file, and an integration test file attests/integration/components/hello-world-test.js
. - Edit the
hello-world.hbs
file and add the hello world string:// app/templates/components/hello-world.hbs Hello World! My name is {{firstName}} {{lastName}}. {{yield}}
The
firstName
andlastName
parameters are retrieved from the component. Theyield
expression is used when the component is in the block form. We'll talk more about this in Chapter 6, Ember Components. - Add two properties to the component file,
hello-world.js
, the first one beingfirstName
and the last one beinglastName
:// components/hello-world.js import Ember from 'ember'; export default Ember.Component.extend({ firstName: 'John', lastName: 'Smith' });
- For the last part, all we need to do is add the component that we just created to one of our
application.hbs
files:// app/templates/application.hbs <h2 id="title">Welcome to Ember</h2> {{hello-world}} {{outlet}}
The
{{hello-world}}
Handlebar expression adds the component to theapplication.hbs
file. Thehello-world
template will then be rendered here. The{{outlet}}
template will render the nested routes under theapplication
route. - Start the Ember server and navigate to
http://localhost:4200
. - After the Ember server is started, open a web browser at localhost port 4200. The message on the screen will show Hello World! My name is John Smith.
- Navigate to
http://localhost:4200/helloworld
and you'll be greeted with two messages. The message on the screen will show Hello World! My name is John Smith. Hello World! My name is Erik Hanchett. - When the
helloworld
route is loaded, the application template is displayed. The{{outlet}}
template then gets rendered with the contents of thehelloworld
template file. This is why both messages are displayed. Remember that all routes are nested under theapplication
route.
How it works...
Ember.js uses the Handlebars templating library. This library provides you with a way to do data binding between the component or controller, also known as a context, and the template. This data binding occurs in both directions. In other words, changes to the data in the component or controller will be reflected in the template. Changes in the template to the data will be reflected in the controller or component.
In the previous simple example, the firstName
and lastName
properties in the component were accessed in the template with double curly braces {{}}
. This is known as a Handlebars expression. The template is just regular HTML with embedded Handlebar expressions. Ember compiles these templates later on during the build process.
- SoapUI Cookbook
- Ext JS Data-driven Application Design
- Magento 2 Theme Design(Second Edition)
- 信息安全技術
- Visual Basic程序設計實驗指導(第4版)
- Bootstrap 4:Responsive Web Design
- 大模型RAG實戰:RAG原理、應用與系統構建
- Learning Laravel's Eloquent
- Java面向對象程序設計
- Learning YARN
- 現代C:概念剖析和編程實踐
- ABAQUS6.14中文版有限元分析與實例詳解
- Instant Automapper
- Java Web動態網站開發(第2版·微課版)
- Learning iOS Penetration Testing