Open In App

Ember.js Service isDestroying Property

Last Updated : 09 Mar, 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 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 isDestroying property of a Service class is a boolean value that indicates whether the service is currently being destroyed. This property is set to true when the willDestroy() lifecycle hook is called on the service, and it remains true until the didDestroy() hook is called.

Syntax: 

this.Object.isDestroying

Returns: It returns the status of the object state that it is being destroyed or not.

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/routes/file1.js

Javascript




import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
  
export default class WebsitesRoute extends Route {
    @service myService;
  
    myObject = this.get('myService.myObject');
    model() {
        console.log(
'Status of isDestroying property Before destroy method : ')
        console.log(this.myObject.isDestroying)
        this.myObject.destroy()
        console.log(
'Status of isDestroying property After destroy method : ')
        console.log(this.myObject.isDestroying)
  
    }
}


app/services/myService.js

Javascript




import Service from '@ember/service';
import EmberObject from '@ember/object';
import Ember from 'ember';
  
export default Service.extend({
    myObject: null,
  
    init() {
        this._super(...arguments);
        this.set('myObject'
            EmberObject.create({ 'hello': 3 }));
    },
});


Output:

output1

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

ember generate route file2

app/route/file2.js

Javascript




import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
  
export default class WebsitesRoute extends Route {
    @service myService;
  
    myObject = this.get('myService.myObject');
    model() {
        console.log(
'Status of isDestroying property Before destroy method : ')
        console.log(this.myObject.isDestroying)
    }
  
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('myObject', this.myObject);
    }
}


app/controllers/file2.js

Javascript




import Controller from '@ember/controller';
import { action } from '@ember/object';
  
export default class ApplicationController extends Controller {
    @action
    destroyMyComponent() {
        this.myObject.destroy()
        console.log('Status of isDestroying property 
           After  destroy method : ')
        console.log(this.myObject.isDestroying)
    }
}


app/services/myService.js

Javascript




import Service from '@ember/service';
import EmberObject from '@ember/object';
import Ember from 'ember';
  
export default Service.extend({
    myObject: null,
  
    init() {
        this._super(...arguments);
        this.set('myObject'
           EmberObject.create({ 'hello': 3 }));
    },
  
    willDestroy() {
        this.get('myObject').destroy();
        this._super(...arguments);
    }
});


app/templates/file2.hbs

HTML




<h1>GeeksforGeeks</h1>
<button {{on "click" this.destroyMyComponent}}>
  Destroy Component</button>


Output:

Output2

Reference: https://api.emberjs.com/ember/4.9/classes/Service/properties/isDestroying?anchor=isDestroying



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads