Open In App

Ember.js EmberRouter decrementProperty() Method

Last Updated : 24 Jan, 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 decrementProperty() function is used to decrement the value of the property to the current value minus the specified amount.

Syntax: 

this.Object.decrementProperty( PropertyName, value) ;

 

Parameters:

  • PropertyName: The property whose value we want to decrease is identified by its name.
  • value: The value that we wish to decrease is that one. The initial setting is 1.

Return: This method returns an object.

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 temp1

app/routes/temp1.js

Javascript




import Route from '@ember/routing/route';
import EmberObject from '@ember/object';
import Ember from 'ember';
  
  
export default class StudentsRoute extends Route {
  
    CreateEmployee() {
        let Employee = EmberObject.create({
            name: '',
            gender: '',
            position: '',
            Salary: 100000,
            email: '',
            mobile: '',
        });
        return Employee;
    }
    setName(data) {
        return this.employee.set('name', data);
    }
    setGender() {
        return this.employee.set('gender', 'M');
    }
    setPosition(data) {
        return this.employee.set('position', data);
    }
    SalaryDecrease(data) {
        return this.employee.decrementProperty('Salary', data);
    }
    SalaryIncrease(data) {
        return this.employee.incrementProperty('Salary', data);
    }
    setEmail(data) {
        return this.employee.set('email', data);
    }
    setMobile(data) {
        return this.employee.set('mobile', data);
    }
    model() {
        this.employee = this.CreateEmployee();
        this.employee.name = this.setName('Yogesh Verma');
        this.employee.gender = this.setGender();
        this.employee.position = this.setPosition('Software Engineer');
        this.employee.email = this.setEmail('yogesh@gmail.com');
        this.employee.mobile = this.setMobile('9382938239');
        this.SalaryDecrease(10000);
        return this.employee;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('employee', this.employee);
    }
}


app/templates/temp1.hbs

HTML




{{page-title "Employee"}}
<h1>Employee Details:</h1>
<div>Name: {{this.employee.name}}</div>
<div>Gender: {{this.employee.gender}}</div>
<div>Position: {{this.employee.position}}</div>
<div>Salary: {{this.employee.Salary}}</div>
<div>Mobile: {{this.employee.mobile}}</div>
<div>Email: {{this.employee.email}}</div>
{{outlet}}


Output:

output1

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

ember generate route temp2

app/routes/temp2.js

Javascript




import Route from '@ember/routing/route';
import EmberObject from '@ember/object';
import Ember from 'ember';
  
const Student = EmberObject.extend({
    changed: Ember.observer('Marks', function () {
        console.log(`Marks Changed`);
    })
});
export default class WebsitesRoute extends Route {
  
    students = [
        Student.create({
            fullName: 'Sam Snehil',
            Marks: 92,
            class: 11,
        }),
        Student.create({
            fullName: 'Ram Sahu',
            Marks: 84,
            class: 10,
        }),
        Student.create({
            fullName: 'Soham Verma',
            Marks: 89,
            class: 12,
        }),
        Student.create({
            fullName: 'David Tigga',
            Marks: 95,
            class: 9,
        }),
        Student.create({
            fullName: 'Pokhu Verma',
            Marks: 95,
            class: 10,
        }),
        Student.create({
            fullName: 'Satyam Verma',
            Marks: 96,
            class: 12,
        }),
    ];
  
    temp;
    model() {
        return this.students;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('students', this.students);
        controller.set('temp', this.temp);
    }
}


app/controllers/temp2.js

Javascript




import Ember from 'ember';
  
export default Ember.Controller.extend({
    actions: {
          
        increaseMarks(data) {
            this.students.forEach((item) =>
                item.incrementProperty('Marks', JSON.parse(data)));
        },
        decreaseMarks(data) {
            this.students.forEach((item) =>
                item.decrementProperty('Marks', JSON.parse(data)));
        },
    },
});


app/templates/temp2.hbs

HTML




{{page-title "incrementProperty"}}
<h3>List of Item in Buckets</h3>
<table>
    <tr>
        <th> Name </th>
        <th>Class </th>
        <th>Marks </th>
    </tr>
    {{#each @model as |temp|}}
    <tr>
        <td>{{temp.fullName}}</td>
        <td>{{temp.class}}</td>
        <td>{{temp.Marks}}</td>
    </tr>
    {{/each}}
</table>
<br />
<div>
    <label>Enter value : </label>
    {{input value=this.item}}
</div><br />
<input
    type="button"
    id="increase-property"
    value="Increase Quantity"
    {{action "increaseMarks" this.item}}
/>
<br /><br />
<input
    type="button"
    id="decrease-property"
    value="decrease Quantity"
    {{action "decreaseMarks" this.item}}
/>
{{outlet}}


Output:

output2

Reference: https://api.emberjs.com/ember/4.9/classes/EmberRouter/methods/decrementProperty?anchor=decrementProperty



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads