Open In App

Ember.js Controller init() Method

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

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.

The init() method is the method that is called whenever the object is created. 

Syntax:

init(){ //algorithm...}; 

Steps to Install and Run Ember.js:

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: Type the following code to generate the route for this example:

ember generate route init1

app/routes/init1.js

Javascript




import Route from '@ember/routing/route';
 
export default class StudentsRoute 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/init1.js

Javascript




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/templates/init1.js

HTML




<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:

Ember.js Controller init() Method

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

ember generate route init2

app/controllers/init2.js

Javascript




import Ember from "ember";
import { addObject, isAny, isEvery } from "@ember/array";
import EmberObject from '@ember/object';
 
const Fruit = EmberObject.extend({
    init() {
        alert(`Name is ${this.get('name')}`);
    }
});
export default Ember.Controller.extend({
    fruits: [Fruit.create({
        'name': 'Lady Finger',
        'isFruit': false,
        'color': 'green'
    }),
    Fruit.create({
        'name': 'Brinjal',
        'isFruit': false,
        'color': 'purple'
    }),
    Fruit.create({
        'name': 'Apple',
        'isFruit': true,
        'color': 'red'
    }),
    Fruit.create({
        'name': 'Grapes',
        'isFruit': true,
        'color': 'green'
    })
    ],
    actions: {
 
        insertItem(data, data1, data2) {
            let temp = Fruit.create({
                name: data,
                isFruit: data1,
                color: data2
            });
            this.fruits.addObject(temp);
        }
    },
});


app/templates/init2.hbs

HTML




{{page-title "Init"}}
<h3>Here is a list of eatables: </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 Fruit"
        {{action "insertItem" this.item1 this.item2 this.item3}}
    />
</div>
{{outlet}}


Output:

Ember.js Controller init() Method

Reference: https://api.emberjs.com/ember/4.6/classes/Controller/methods/init?anchor=init



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads