Key Points
- Ember component is the core of Ember applications, allowing developers to define custom, application-specific HTML tags and implement their behavior using JavaScript. In Ember 2.x, components replace views and controllers.
- The Ember component contains a Handlebars template file and a matching Ember class. These components can be used with other components, and can even be nested in parent components and have properties similar to native HTML elements.
- Dynamic data can be added to Ember applications through a model (the object representing the underlying data the application presents to the user). This allows for the creation of interactive and dynamic components.
- User interaction can be added to a component using actions (actions sent to the component class). These actions can be used to create interactive elements, such as clickable tabs that display different content.
This article was peer-reviewed by Edwin Reynoso and Nilson Jacques. Thanks to all the peer reviewers of SitePoint to get the content of SitePoint to its best! The components are an important part of the Ember application. They allow you to define your own, application-specific HTML tags and implement their behavior using JavaScript. Starting with Ember 2.x, components will replace views and controllers (deprecated) and are the recommended way to build Ember applications.
Ember's component implementation follows the W3C Web component specifications as much as possible. Once custom elements are widely available in the browser, it should be easy to migrate Ember components to the W3C standard and make them usable by other frameworks.If you want to learn more about why the routable components replace controllers and views, check out this short video by Ember core team members Yehuda Katz and Tom Dale.
Tab Switcher Application
To gain insight into the Ember component, we will build a tab switcher widget. This will contain a set of tabs with relevant content. Clicking on a tab will display the contents of that tab and hide the contents of other tabs. Simple enough? Let's get started.As always, you can find the code for this tutorial on our GitHub repository or on this Ember Twiddle if you want to experiment with the code in your browser.
Composition of Ember component
The Ember component contains a Handlebars template file and a matching Ember class. This class is only needed to be implemented when we need additional interaction with the components. Components are used in a similar way to ordinary HTML tags. When we build the tab switcher component, we will be able to use it like this:
<code>{{tab-switcher}}{{/tab-switcher}}</code>The template file of the Ember component is located in the app/templates/components directory. The class file is located in app/components. We use all lowercase letters, separated by hyphen between words to name the Ember component. This is named as per convention, which can avoid name conflicts with future HTML Web components.
Our main Ember component will be tab-switcher. Note that I'm talking about the main component, because we will have multiple components. You can use components in conjunction with other components. You can even nest components in another parent component. In the case of our tab-switcher, we will have one or more tab-item components as shown below:
<code>{{tab-switcher}}{{/tab-switcher}}</code>
As you can see, components can also have properties like native HTML elements.
Create Ember 2.x project
To follow this tutorial, you need to create an Ember 2.x project. The method is as follows:
Ember is installed using npm. See here for tutorials on npm.
<code>{{#each tabItems as |tabItem| }} {{tab-item item=tabItem setSelectedTabItemAction="setSelectedTabItem" }} {{/each}}</code>
At the time of writing this article, this will introduce version 1.13
<code>npm install -g ember-cli </code>
Next, create a new Ember application:
<code>ember -v => version: 1.13.8 </code>
Navigate to this directory and edit the bower.json file to contain the latest versions of Ember, ember-data, and ember-load-initializers:
<code>ember new tabswitcher</code>
Back to terminal and run:
<code>{ "name": "hello-world", "dependencies": { "ember": "^2.1.0", "ember-data": "^2.1.0", "ember-load-initializers": "^ember-cli/ember-load-initializers#0.1.7", ... } } </code>
Bower may prompt you to parse the version of Ember. Select version 2.1 from the provided list and prefix it with an exclamation mark to persist its resolution to bower.json.
Next to start the development server of Ember CLI:
<code>bower install </code>
Last navigate to http://localhost:4200/ and check the version of the browser console.
Create a tab switcher component
Let's create a tab switcher component using Ember's built-in generator:
<code>ember server</code>
This will create three new files. One is our HTML Handlebars file (app/templates/components/tab-switcher.hbs), the second is our component class JavaScript file (app/components/tab-switcher.js), and the last is the test file (tests/integration/components/tab-switcher-test.js). Test components are not within the scope of this tutorial, but you can read more about it on the Ember website.
Now run ember server to load the server and navigate to http://localhost:4200/. You should see a welcome message titled "Welcome to Ember". So why are our components not displayed? Well, we haven't used it yet, so let's use it now.
Using components
Open the application template app/templates/application.hbs. Add the following after the h2 tag to use the component.
<code>ember generate component tab-switcher</code>
In Ember, components can be used in two ways. The first method, called the inline form , is to use them without any content. That's what we do here. The second method is called the block form , which allows the Handlebars template to be passed to the component and rendered the template where the {{yield}} expression appears in the component template. In this tutorial, we will stick to the inline form.
However, this still doesn't show anything on the screen. This is because the component itself has nothing to display. We can change this by adding the following line to the component's template file (app/templates/components/tab-switcher.hbs):<code>{{tab-switcher}}{{/tab-switcher}}</code>
Now, when the page reloads (which should happen automatically), you will see the text shown above. An exciting moment!
Create tab project components
Now that we have set up our main tab-switcher component, let's create some tab-item components to nest in it. We can create a new tab-item component like this:
<code>{{#each tabItems as |tabItem| }} {{tab-item item=tabItem setSelectedTabItemAction="setSelectedTabItem" }} {{/each}}</code>
Now change the Handlebars file of the new component (app/templates/components/tab-item.hbs) to:
<code>npm install -g ember-cli </code>
Next, let's nest three tab-items in our main tab-switcher component. Change the tab-switcher template file (app/templates/components/tab-switcher.hbs) to:
<code>ember -v => version: 1.13.8 </code>
As mentioned above, the yield helper function will render any Handlebars template passed to our component. However, this is only useful if we use tab-switcher in block form. Since we didn't do this, we can completely remove the yield helper function.
Now, when we look at the browser, we will see three tab-item components, all of which display "Tab Items Title". Our component is now quite static, so let's add some dynamic data.
(The rest is similar to the previous output, except that the paragraphs are reorganized and worded to maintain content consistency and avoid duplication. To save space, the output of the remaining part is not repeated here.)
The above is the detailed content of Understanding Components in Ember 2. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),