javascript - 在 Emberjs 中删除记录

标签 javascript ember.js ember-data

我正在努力关注ember 2.0's documentation用于删除记录然后重定向到新的 url。当我尝试时,控制台出现以下错误:

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. 

我的文件如下。

路线:

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;

路线/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){
          })
      }
  }
});

模板/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}}

Controller /*

all missing except for pencilcreate.js, not applicable here

模板/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>

路线/pencil.js

export default Ember.Route.extend({
  model() {
    return this.store.findAll('pencil');
  }  
});

最佳答案

这是另一个答案的版本,只是稍微收紧了一点。

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

关于javascript - 在 Emberjs 中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32851565/

相关文章:

backbone.js - RequireJS - 为什么我应该填充 jquery、backbone 等库

ember.js - 您可以预加载相关数据,以便将您的关系缓存在 ember-data 中吗?

javascript - transient 和非脏属性,ember-data

ember.js - 将 JSON 对象加载到 Ember Data 中,例如在加载时将 JSON 嵌入页面并填充商店

javascript - 除了变量定义之外,javascript 中是否需要 "this"

javascript - 输入 :text + textarea selector in jquery

javascript - 构建双功能按钮

javascript - 无法捕获来自 ember backgroundReload 的错误

javascript - 在 HTML 页面或 WordPress 编辑器中突出显示不间断空格

twitter-bootstrap - 如何使用 Ember.js 和 Handlebars.js 渲染(Twitter Bootstrap)网格?