0

I am struggling to follow ember 2.0's documentation for deleting records and then redirecting to a new url. When I try, I get the following error to the console:

Error while processing route: pencils Attempted to handle event `pushedData` on <name-emberjs@model:pencil::ember523:null> while in state root.deleted.inFlight.  Error: Attempted to handle event `pushedData` on <name-emberjs@model:pencil::ember523:null> while in state root.deleted.inFlight. 

My files follow.

Routes:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
    this.resource('pencilview', { path: '/pencils/:pencil_id' });
    this.resource('pencilcreate', { path: '/pencils/new' });
    this.resource('pencils');
});


export default Router;
import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
    this.resource('pencilview', { path: '/pencils/:pencil_id' });
    this.resource('pencilcreate', { path: '/pencils/new' });
    this.resource('pencils');
});


export default Router;

routes/pencilview.js

export default Ember.Route.extend({
    model: function(params) {
      return this.store.find('pencil', params.pencil_id);
    },

    actions: {
        save(pencil){
            this.store.find('pencil', pencil.id).then(function(pencil){
                pencil.save();
            })
            this.transitionTo('pencils');
        },

        remove(id){
            this.get('store').find('pencil', id).then(function(pencil2){
                pencil2.destroyRecord();
            });
            this.transitionTo('pencils');
        },

      cancel(pencil){
          this.store.find('pencil'.pencil.id).then(function(pencil){
          })
      }
  }
});

templates/pencilview.hbs

<h2>Single Pencil View</h2>
<p>Pencil ID: {{model.id}}</p>

<p>
<label for='name'>Name</label>
{{input type="text" id="name" value=model.name}}</p>

<p>
<label for='name2'>Name2</label>
{{input type="text" id="n2" value=model.n2}}</p>

<p>
<label for='name3'>Name3</label>
{{input type="text" id="n3" value=model.n3}}</p>

<p><button {{action "remove" model.id}}>Delete</button></p>
<p><button {{action "save" model}}>Save</button></p>

{{#link-to 'pencils'}}Pencils{{/link-to}}

controllers/*

all missing except for pencilcreate.js, not applicable here

template/pencils.hbs

<h2>All Pencils</h2>
<p>{{#link-to 'pencilcreate' (query-params direction='pencils') class='btn btn-default' }}Create New Pencil{{/link-to}}</p>

<table id='pencil_allTable' class='display'>
<thead><tr><td>ID</td><td>Brand</td><</tr></thead>
<tbody>
{{#each model as |pencil|}}
  <tr>
    <td>{{#link-to "penciliew" pencil class='btn btn-default'}}{{pencil.id}}{{/link-to}}</td>
    <td>{{pencil.brand}}</td>
  </tr>
{{/each}}
</tbody></table>

<script>
$(document).ready( function () {
    $('#pencil_allTable').DataTable();
    } );
</script>

routes/pencil.js

export default Ember.Route.extend({
  model() {
    return this.store.findAll('pencil');
  }  
});
8
  • It seems that both Henry and torazaburo's answers do not update my drugs view. That is, the delete api is hit, but the store is not updated. When transitioning to pencils, it shows the old data unless I refresh the page. I will award the bounty to whomever updates their answer and solves this final puzzle piece first. If a new answer jumps in with a conglomerate of those listed below, I will check mark it and give Henry the bounty. Commented Oct 4, 2015 at 6:23
  • I see nothing anywhere about any drugs view. If you mean pencils, please show the route, controller ,and template. Commented Oct 4, 2015 at 7:00
  • My guess is that your server is not deleting the record properly. So the findAll is bringing it back. Please consult your server logs etc., examine your database, etc. Perhaps there's some kind of caching somewhere along the line. Commented Oct 4, 2015 at 11:24
  • Also, check the network payload resulting from the findAll. I preduct you'll find it contains the allegedly deleted pencil. Commented Oct 4, 2015 at 11:40
  • 1
    You can force your model to retrieve always fresh data by this.store.findAll('pencil', { reload: true }); Commented Oct 6, 2015 at 9:39

3 Answers 3

8
+50

This is a version of another answer, just tightened up a bit.

remove(id) {
  this.get('store').find('pencil', id) . 
    then(pencil2 => pencil2.destroyRecord())
    .then(() => this.transitionTo('pencils'));
}
Sign up to request clarification or add additional context in comments.

1 Comment

please see my comment above for a further refinement request of your answer.
3

You've been bitten by this scope in:

this.transitionTo('pencils').then(function(id){
    this.get('store') // <== this === undefined

This code should work:

remove(id) {
    this.get('store').find('pencil', id).then(pencil2 => {
        pencil2.deleteRecord();
    });
    this.transitionTo('pencils');
},

3 Comments

This is leading to a GET http://0.0.0.0:6543/api/pencils/%3Cname-emberjs%40route%3Apencils%3A%3Aember590%3E 500 (Internal Server Error). I'm wondering if save() needs to be run before the transitionTo as well but this creates a different problem: Error while processing route: pencils Attempted to handle event pushedData on <name-emberjs@pencil::ember549:3> while in state root.deleted.inFlight. Error: Attempted to handle event pushedData on <name-emberjs@model:pencil::ember549:3> while in state root.deleted.inFlight.
Answer updated. You can't fetch deleted record, so you can't save it after it has been deleted.
This only partially works. calling deleteRecord() doesn't send the DELETE request to the api; it only affects the local store. Changing deleteRecord() to destroyRecord() causes it to bug out again, logging root.deleted.inFlight error.
3

Let the promise fulfil itself:

remove(id){
        this.get('store').find('pencil', id).then((pencil2) => {
            pencil2.destroyRecord().then(() => {
               this.transitionTo('pencils');
           });
        });           
    },

Edit: getting rid of the const _this.

2 Comments

Good answer, plus one, but minor nitpicking, since we went to all the trouble to be able to use ES6 and fat arrows, can't we tighten up the code and get rid of _this all at the same time? Also, it might be slightly more readable to string out the thens instead of nesting them.
please see my comment above for a further refinement request of your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.