Delete Record in Emberjs

偶尔善良 提交于 2019-12-25 06:07:09

问题


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');
  }  
});

回答1:


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'));
}



回答2:


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:


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.



来源:https://stackoverflow.com/questions/32851565/delete-record-in-emberjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!