How to destroy a route model on transition in ember.js?

by hal.littel , in category: Javascript , a month ago

How to destroy a route model on transition in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , a month ago

@hal.littel 

In Ember.js, you can destroy a route model on transition by using the resetController hook in your route file. Here's an example of how you can accomplish this:

1
2
3
4
5
6
7
8
9
import Route from '@ember/routing/route';

export default Route.extend({
  resetController(controller, isExiting, transition) {
    if (isExiting) {
      controller.get('model').destroyRecord();
    }
  }
});


In this example, we are overriding the resetController hook in our route file. When the route is exiting (i.e., transitioning to a different route), this hook will be called. Inside the hook, we are retrieving the model from the controller and calling the destroyRecord method on it. This will delete the model from the backend server.


Make sure to replace model with the actual name of your model if it's different. This code should be added to the route file where you want to destroy the model on transition.