Ember.js Route willDestroy() Method
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 many websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.
The willDestroy() Method of class Route is a function that will call whenever all the resources allocated to the object are released or in other words when the object is destroyed.
Syntax:
willDestroy(){
// Function Algorithm
}
Parameters: It doesn’t take any parameters.
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 server
Example 1: Type the following code to generate the route for this example:
ember generate route test1
app/routes/test1.js
Javascript
import Route from "@ember/routing/route" ;
import EmberObject from '@ember/object' ;
import ObjectProxy from '@ember/array/proxy' ;
import { A } from '@ember/array' ;
export default class ListRoute extends Route {
object = EmberObject.create({
name: 'Sam Snehil' ,
Gender: 'M' ,
Occupation: 'Lawyer' ,
Salary: 89000,
mobile: 82982472928,
email: 'samsnehil@gmail.com' ,
willDestroy() {
console.log( 'Object is About to die!!!' );
},
});
person = ObjectProxy.create( this .object
);
idx;
model() {
return this .person;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( "person" , this .person);
controller.set( "list1" , this .list1);
}
}
|
app/controllers/test1.js
Javascript
import Ember from "ember" ;
import {
clear, insertAt, indexOf, lastIndexOf,
includes
} from "@ember/array" ;
export default Ember.Controller.extend({
actions: {
delete () {
this .person.destroy();
},
showDestroyed() {
console.log( this .person.get( 'isDestroyed' ))
}
},
});
|
app/templates/test1.hbs
HTML
{{page-title "ObjectProxy"}}
< table >
< h1 >Employee's Details:</ h1 >
< div >< b >Name:</ b > {{this.person.name}}</ div >
< div >< b >Gender:</ b > {{this.person.Gender}}</ div >
< div >< b >Occupation:</ b > {{this.person.Occupation}}</ div >
< div >< b >Mobile:</ b > {{this.person.mobile}}</ div >
< div >< b >Email:</ b > {{this.person.email}}</ div >
< div >< b >Salary:</ b > {{this.person.Salary}}</ div >
</ table >
< br />< br />
< input
type = "button"
id = "Destroy"
value = "Release all Resource"
{{action 'delete'}}
/>< br />< br />
< input
type = "button"
id = "show"
value = "show isDestroyed"
{{action 'showDestroyed'}}
/>
{{outlet}}
|
Output:
output1
Example 2: Type the following code to generate the route for this example:
ember generate route test2
app/routes/test2.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' ,
willDestroy() {
console.log( 'Object is About to die!!!' );
}
})
model() {
return this .person;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( 'person' , this .person);
}
}
|
app/controllers/test2.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();
},
print() {
console.log( 'Value of isDestroyed : '
+ this .person.isDestroyed)
},
}
});
|
app/templates/test2.js
HTML
{{page-title "ObjectProxy "}}
< 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 = "Destroy"
value = "Destroy Object"
{{action 'des'}}
/>< br />< br />
< input
type = "button"
id = "CacheFor"
value = "isDestroyed"
{{action 'print'}}
/>
{{outlet}}
|
Output:
Output2
Reference: https://api.emberjs.com/ember/4.9/classes/Route/methods/willDestroy?anchor=willDestroy
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...