Open In App

Ember.js Controller Class

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

Controller Class:



In Ember.js, you can add display logic to your models using controllers. Models typically have attributes that need to be saved to the server, whereas controllers typically have properties that your app can ignore saving to the server.

 



Methods: The following is the list of methods for this class:

Properties: The following is the list of the properties of this class:

Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install ember-cli

Step 2: Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

To start the server, type:

ember serve

Example 1: In this example, I am going to demonstrate the use of the function getProperties of the class.

Type the following code to generate the route for this example:

ember generate route fruits

app/routes/fruits.js




import Route from '@ember/routing/route';
  
export default class FruitsRoute extends Route {
    fruits = [
        {
            'name': 'Lady Finger',
            'isFruit': false,
            'color': 'green'
        },
        {
            'name': 'Brinjal',
            'isFruit': false,
            'color': 'purple'
        },
        {
            'name': 'Apple',
            'isFruit': true,
            'color': 'red'
        },
        {
            'name': 'Grapes',
            'isFruit': true,
            'color': 'green'
        },
        {
            'name': 'Mango',
            'isFruit': true,
            'color': 'yellow'
        },
        {
            'name': 'Watermelon',
            'isFruit': true,
            'color': 'red'
        },
        {
            'name': 'Orange',
            'isFruit': true,
            'color': 'orange'
        }
    ];
    item2;
    item3;
  
    model() {
        this.set('[]', this.fruits)
        return this;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('fruits', this.fruits);
    }
}

app/controllers/fruits.js




import Ember from "ember";
import { addObject, isAny, isEvery } from "@ember/array";
import EmberObject from '@ember/object';
  
const Fruit = EmberObject.extend({
    init() {
        alert(`${this.getProperties('name').name}
        is Added in list`);
    }
});
export default Ember.Controller.extend({
    actions: {
        list() {
            let list = '';
            this.fruits.map((item) => list +=
                item.name + ` is ${item.color}` + '\n');
            alert(list)
        },
        insertItem(data, data1, data2) {
            let temp = Fruit.create({
                name: data,
                isFruit: data1,
                color: data2
            });
            this.fruits.addObject(temp);
        }
    },
});

app/template/fruits.hbs




<h3>List of Fruits:</h3>
<table>
    <tr>
        <th>Name</th>
        <th>Fruit</th>
        <th>Color</th>
    </tr>
    {{#each this.fruits as |detail|}}
        <tr>
            <td>{{detail.name}}</td>
            <td>{{detail.isFruit}}</td>
            <td>{{detail.color}}</td>
        </tr>
    {{/each}}
</table>
  
<br /><br />
<div>
    <label>Enter Fruit Name: </label>
    {{input value=this.item1}}
</div><br />
<div>
    <label>Item is Fruit: </label>
    {{input value=this.item2}}
</div><br />
<div>
    <label>Enter Fruit color: </label>
    {{input value=this.item3}}
</div><br />
<div>
    <input
        type="button"
        id="insert-item"
        value="Insert Furit"
        {{action "insertItem" this.item1 this.item2 this.item3}}
    />
</div>
<br />
<div>
    <input
        type="button"
        id="list-item"
        value="Print Items"
        {{action "list"}}
    />
</div>
{{outlet}}

Output:

 

Example 2: In this example, I am going to demonstrate the use of the function init of the class.

Type the following code to generate the route for this example.

ember generate route party

app/routes/party.js




import Route from '@ember/routing/route';
  
export default class PartyRoute extends Route {
    p1 = [
        'Digital Camera',
        'Jugs, cups & straws',
        'Balloons',
        'Scissors',
        'Cold Drink',
        'Table',
    ];
    model() {
        return this.p1;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('p1', this.p1);
    }
}

app/controllers/party.js




import Ember from 'ember';
  
export default Ember.Controller.extend({
    value: 'value',
    init() {
        this._super(...arguments);
        this.addObserver('value', this, 'Change');
        alert('Controller Init() is initiated');
    },
  
    Change() {
        console.log('value changed');
    },
    actions: {
        remove(data) {
            this.p1.set('[]', this.p1.without(data));
        },
        print() {
            let ans = this.p1.set('[]');
            alert(ans.join('\n'));
        },
    },
});

app/template/party.hbs




<ul>
    <h3>List is : </h3>
    {{#each @model as |party|}}
    <li>{{party}}</li>
    {{/each}}
</ul>
<br />
{{input value=this.value}}
<br />
<input type="button" 
       id="check-atIndex" 
       value="Remove" {{action "remove" this.value}} />
<br /><br />
<input type="button" 
       id="print-item"
       value="Print All Items" 
       {{action "print" this.value}} />

Output:

 

Reference: https://api.emberjs.com/ember/2.15/classes/Ember.Controller


Article Tags :