Open In App

Explain the core concept of Ember.js

Last Updated : 27 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Ember.js is a JavaScript front-end framework for building single-page web applications. It is designed to make it easy to build complex, data-driven web applications by providing a powerful set of features and tools that handle many of the common tasks involved in web development.

One of the core concepts of Ember.js is the separation of concerns between the different parts of a web application. Ember.js uses a Model-View-ViewModel (MVVM) architecture, in which the model represents the data in the application, the view is responsible for rendering the user interface, and the view model acts as a mediator between the model and the view.

Ember.js also has a strong emphasis on convention over configuration, meaning that it provides a set of default conventions for structuring an application, and developers can customize these conventions if needed. This helps to reduce the amount of boilerplate code that needs to be written and makes it easier to build consistent, maintainable applications.

Let’s understand some of the core concepts in Ember.js.

1. Templates: Templates in Ember.js are used to define the user interface of an application. They are written in the Handlebars template language, which is a simple, expressive syntax for defining dynamic templates. In Ember.js, templates are typically associated with a route or a component. When a route or component is rendered, the associated template is used to generate the HTML that is displayed to the user.

2. Routes: In Ember.js, routes are responsible for defining the URL paths in an application and mapping them to specific templates and controllers. They are an important part of the Ember.js routing system, which is designed to make it easy to organize and navigate an application. Overall, routes in Ember.js are an important part of the framework’s routing system and are used to define the URL paths in an application and map them to specific templates and controllers. They help to organize and navigate an application and allow you to pass data to a route via the URL.

3. Components: In Ember.js, components are reusable UI elements that can be easily shared between different parts of an application. They have their own templates, data, and behavior, and are designed to be easily composable. Overall, components in Ember.js are a powerful way to create reusable UI elements that can be easily shared between different parts of an application. They can have their own templates, data, and behavior, and are designed to be easily composable.

4. Controllers: In Ember.js, a controller is a JavaScript object that is responsible for maintaining the state of a specific route or a specific part of the application. Controllers are used to managing the data for a template, and they can be used to provide the logic that determines how the data is displayed or manipulated. Controllers in Ember.js are typically associated with a route, and they are responsible for managing the data that is used by the template for that route. When a user navigates to a route, the corresponding controller is instantiated and its setup method is called, which is responsible for setting up the data that the template will use. Controllers can also be nested, which allows you to manage more complex application states. For example, if you have a route for a specific user, you might nest a controller for that user under the main application controller, and the nested controller would be responsible for managing the data for that specific user.

5. Models: In Ember.js, a model is a JavaScript object that represents the data for a specific record or entity in your application. Models are typically used to represent data that is persisted in a database, but they can also be used to represent in-memory data or data that is fetched from a remote API. Models in Ember.js are defined using Ember.Object class, and they can have properties and methods just like any other JavaScript object. You can use the Ember.get and Ember.set functions to get and set the values of model properties, and you can use computed properties to define properties that are derived from other properties.

Let’s understand the above concept using an example. You can refer to this article if you want to learn how to setup ember.js project. 

 Example: Create an Ember.js application with a route called “test1”. This route should have a model that includes a list of party items and a controller with actions to display the first and last items in the list, show the length of the list, exclude a specified item from the list, and display all items in the list. The route should also have a template with input fields and buttons to trigger the actions in the controller. Use a component to display the list of party items in the template.

Step 1: Type the below command into your terminal or windows command  to generate the route for this example:

ember generate route test1

app/routes/test1.js

Javascript




import Route from "@ember/routing/route";
  
export default class StudentsRoute extends Route {
    partyItems = [
        'Digital Camera',
        'Jugs, cups & straws',
        'Balloons',
        'Scissors',
        'Cold Drink',
        'Table Confetti',
        'Party Hats',
        'Wine',
        'Napkins',
        'Party Plates',
        'Speakers',
        'Music System',
        'Cups',
    ];
    len;
    model() {
        return this.partyItems;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set("partyItems", this.partyItems);
        controller.set("len", this.len);
        controller.set("item", this.item);
    }
}


app/controllers/test1.js

Javascript




import Ember from "ember";
  
export default Ember.Controller.extend({
    actions: {
        show_first() {
            let ans = this.partyItems.get('firstObject');
            alert(ans)
        },
        show_last() {
            let ans = this.partyItems.get('lastObject');
            alert(ans)
        },
        show_len() {
            let S_len = this.partyItems.length;
            this.set('len', S_len)
            alert('Length of List is ' + this.len);
        },
        check_items(data) {
            let temp = this.partyItems.without(data)
            alert(temp.join('\n'))
        },
        show() {
            let temp = this.partyItems.get('[]');
            alert(temp.join('\n'))
        },
  
    },
});


app/templates/test1.hbs

Javascript




{{page-title "[ ]"}}
<h3>List of Items: </h3>
<table>
    <ul>
        {{#each @model as |student|}}
              <li>{{student}}</li>
        {{/each}}
      </ul>
</table>
<br /><br />
<div>
    <label>Enter Item: </label>
      {{input value=this.temp}}
</div>
<input
    type="button"
      id="check-atIndex"
      value="Print Except this Item"
      {{action "check_items" this.temp}}
/>
<br /><br />
<input
    type="button"
      id="show-item"
      value="Pop up All Items"
      {{action "show"}}
/>
<br /><br />
<input
    type="button"
      id="first-item"
      value="Show First Item"
      {{action "show_first"}}
/>
<br /><br />
<input
    type="button"
      id="show-item2"
      value="Show Last Item"
      {{action "show_last"}}
/>
  
<br /><br />
<input
    type="button"
      id="print-list"
      value="Print length of List"
      {{action "show_len"}}
/>
{{outlet}}


Output: Visit localhost:4200/test1 to view the output.

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads