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

by hal.littel , in category: Javascript , 8 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 8 months 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.

Related Threads:

How to pass a model value to a new route in ember.js?
How to create controller with own model without route in ember.js?
How to handle more than one model into a template/route in ember.js?
What is the difference between route and path in ember.js?
How to configure a root route in ember.js?
How to reload current route in ember.js?