Reputation: 1856
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');
}
});
Upvotes: 0
Views: 2203
Reputation:
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'));
}
Upvotes: 8
Reputation: 1281
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
.
Upvotes: 3
Reputation: 18682
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');
},
Upvotes: 3