Level: Beginner

Abhilash L R
Jun 1, 2018 · 1 min read

Supposing that you have an index route which displays a list of all records of book and another route that creates a record for the same, then the newly created record will also be displayed as an empty item. For example: https://ember-twiddle.com/5b3175b92732d04e6132040271d0b911?openFiles=templates.application.hbs%2C

EmberJS Cookbook is a valid book while there is an orphaned empty record displayed during listing!

Since you are creating a new model it remains as an orphan and you need to manually destroy it (roll it back). This can be done in the route’s resetController hook. Something like below:

import Route from '@ember/routing/route';
import { get } from '@ember/object';
export default Ember.Route.extend({
model() {
return get(this, 'store').createRecord('some-model');
resetController(controller, isExiting) {
if (isExiting) {
get(controller, 'model').rollbackAttributes();

Refer: https://emberjs.com/api/ember-data/3.1/classes/DS.Model/methods/rollbackAttributes?anchor=rollbackAttributes

Version: 1.12+

PS: Clap if you liked, and don’t forget to follow me here: Abhilash or on Twitter @abhilashlr


Easy code samples to improve your Ember Engineering productivity

Abhilash L R

Written by

Loves to travel, writes about programming, photography, life lessons and sometimes cooking


Easy code samples to improve your Ember Engineering productivity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade