Open In App

Ember.js Component actions Property

Last Updated : 12 Apr, 2023
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 are 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 actions  Property of the Component class is the property that is used to define the action which will be available on ActionHandler as action targets. These functions will be invoked when a matching action is triggered from the templates. 

Syntax: 

@action
function(){// function logic}

Parameters:

  • function: It is the name function to define. 

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 file1

app/components/third.js

Javascript




import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import Ember from 'ember';
 
export default Ember.Component.extend({
    @tracked
    name: 'Gulshan Sahu',
    @tracked
    gender: 'M',
    @tracked
    class: 12,
    @tracked
    grade: 'A',
    @tracked   
    salary: 78000,
    @tracked
    mobile: 983938494,
    @tracked
    email: 'gulshasahu@gmail.com',
    @action
    increase(){
        this.incrementProperty('salary', 1000);
    },
    @action
    decrease() {
        this.decrementProperty('salary', 1000);
    },
})


app/components/third.hbs

HTML




{{page-title "Component action"}}
{{yield}}
<div>Name: {{this.name}}</div>
<div>Gender: {{this.gender}}</div>
<div>Grade: {{this.grade}}</div>
<div>Class: {{this.class}}</div>
<div>Salary: {{this.salary}}</div>
<div>Mobile: {{this.mobile}}</div>
<div>Email: {{this.email}}</div>
 
<br />
<br /><br />
<input
    type="button"
    id="increment-money"
    value="Increase Money"
    {{action "increase"}}
/>
<br /><br />
<input
    type="button"
    id="decrement-money"
    value="Decrease Money"
    {{action "decrease"}}
/>
{{outlet}}


app/file1.js

HTML




<Third>
    <h1>Person's Details :</h1>
</Third>


Output:

output1

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

ember generate route file2

app/components/fourth.js

Javascript




import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import Ember from 'ember';
import { action } from '@ember/object';
import ArrayProxy from '@ember/array/proxy';
import { A } from '@ember/array';
 
export default class extends Component {
    @tracked
    item;
    @tracked
    value = Food.create({
        value: 0,
    });
    @tracked
    list1 = ["a", "a", "v", "c", "c", "a",
        "d", "q", "b", "z", "a"];
 
    @tracked
    list = ArrayProxy.create({
        content: A(this.list1),
    })
 
    @action
    removeItem(item) {
        this.list.removeObject(item);
    }
 
    @action
    rotate() {
        this.list.reverseObjects();
    }
}


app/components/fourth.hbs

HTML




{{page-title "Component action"}}
<h3>Here is a list of Items: </h3>
<ul>
    {{#each this.list as |i|}}
    <li>{{i}}</li>
    {{/each}}
</ul>
<br />
<div>
    <label>Enter Index: </label>
    {{input value=this.item}}
</div>
<div>
    <input type="button"
     id="remove-item"
     value="remove Item"
     {{action "removeItem" this.item}} />
</div>
 
<br />
<div>
    <input type="button"
     id="rotate-item"
     value="rotate Item"
     {{action "rotate" }} />
</div>
{{outlet}}


app/templates/file.hbs

HTML




<Fourth>
    This is Main templates class
</Fourth>


Output:

Output2

 Reference: https://api.emberjs.com/ember/4.9/classes/Component/properties/actions?anchor=actions



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads