Open In App

Ember.js ArrayProxy destroy() Method

Last Updated : 05 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 destroy() Method of ArrayProxy is used to destroy the object. This method destroys the object by setting the isDestroyed flag and delete all of its metadata. 

Syntax: 

this.Object.destroy() ; 

Parameters: It doesn’t take any parameters. 

Return type: 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 ArrayProxy from '@ember/object';
import { computed } from '@ember/object';
  
const Person = ArrayProxy.extend({
    fullName: computed('firstName', 'lastName', function () {
        return this.get('firstName') + ' '
            + this.get('lastName');
    }),
    toStringExtension() {
        return this.get('fullName');
    }
});
  
export default class StudentsRoute extends Route {
    person = Person.create({
  
        firstName: 'Aman',
        lastName: 'Verma',
        gender: 'M',
        class: 10,
        marks: 83,
        mobile: '8399384948',
        grade: 'A',
  
    })
  
    model() {
        return this.person;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('person', this.person);
    }
}


app/controllers/temp1.js

Javascript




import Ember from 'ember';
import EmberObject from '@ember/object';
import { cacheFor, destroy } from '@ember/object';
  
export default Ember.Controller.extend({
    actions: {
        des() {
            let t1 = this.person.destroy();
            console.log(t1 + ' is Destroyed ')
        },
        print() {
            console.log('Value of isDestroyed : ' 
                + this.person.isDestroyed)
        },
    }
});


app/templates/temp1.hbs

HTML




{{page-title "ArrayProxy"}}
<table>
    <h1>Student's Details:</h1>
    <div><b>Name:</b> {{this.person.fullName}}</div>
    <div><b>Gender:</b> {{this.person.gender}}</div>
    <div><b>Class:</b> {{this.person.class}}</div>
    <div><b>Mobile:</b> {{this.person.mobile}}</div>
    <div><b>Grade:</b> {{this.person.grade}}</div>
    <div><b>Marks:</b> {{this.person.marks}}</div>
</table>
<br /><br/>
<input
    type="button"
    id="CacheFor"
    value="Destroy Details"
    {{action 'des'}}
/><br /><br/>
<input
    type="button"
    id="CacheFor"
    value="Print Ember Class"
    {{action 'print'}}
/>
{{outlet}}


Output:

destroy() method 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 ArrayProxy from '@ember/object/proxy';
  
const Employee = ArrayProxy.extend({
    toStringExtension() {
        return this.get('fullName');
    }
});
  
export default class DetailsRoute extends Route {
    person;
    createPerson() {
        let person = Employee.create({
            name: 'Rahul Singh',
            age: 24,
            mobile: '1298119967',
            country: 'India',
            language: {
                0: ['Bengali']
            },
            salary: 10000,
            gender: 'M',
            zipCode: '800020',
        });
        return person
    }
  
    model() {
        this.person = this.createPerson();
        return this.person
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('person', this.person);
    }
}


app/controllers/temp2.js

Javascript




import Ember from 'ember';
import EmberObject from '@ember/object';
  
const Employee = EmberObject.extend({
    concatenatedProperties: ['work', 'contact'],
    work: {
        0: ['Developing Natural Language Processing']
    },
    contact: {
        phone: ['9839281832']
    }
});
export default Ember.Controller.extend({
  
    actions: {
  
        des() {
            let t1 = this.person.destroy();
            console.log(t1 + ' is Destroyed ')
        },
        print() {
            console.log('Value of isDestroyed : '
                + this.person.isDestroyed)
        },
        IncreaseSalary() {
            let temp = this.person.salary + 1000;
            this.person.set('salary', temp);
        }
    }
});


app/templates/temp2.hbs

HTML




{{page-title "ArrayProxy"}}
<table>
    <h1>Employee's Details: </h1>
    <div><b>Name:</b> {{this.person.name}}</div>
    <div><b>Gender:</b> {{this.person.gender}}</div>
    <div><b>Salary:</b> {{this.person.salary}}</div>
    <div><b>Mobile No.:</b>{{this.person.mobile}}</div>
    <div><b>Zip Code:</b> {{this.person.zipCode}}</div>
    <div><b>Country:</b> {{this.person.country}}</div>
    <div><b>Age:</b> {{this.person.age}}</div>
    <div><b>Work Under:</b>
        {{#each-in this.person.language as |key value|}}
        <li>{{value}}</li>
        {{/each-in}}
    </div>
</table>
  
<br /><br/>
<input
    type="button"
    id="CacheFor"
    value="Destroy Details"
    {{action 'des'}}
/><br /><br/>
<input
    type="button"
    id="CacheFor"
    value="Print isDestroyed Property"
    {{action 'print'}}
/>
<br /><br/>
<input
    type="button"
    id="Change"
    value="Increase Salary"
    {{action 'IncreaseSalary'}}
>
{{outlet}}


Output:

destroyed() method output

Reference: https://api.emberjs.com/ember/4.9/classes/ArrayProxy/methods/destroy?anchor=destroy



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads